diff --git a/Assignment/Node.cpp b/Assignment/Node.cpp new file mode 100644 index 0000000..97dab45 --- /dev/null +++ b/Assignment/Node.cpp @@ -0,0 +1,87 @@ +/* Copyright (C) 2018 +* Course: CO2003 +* Author: Rang Nguyen +* Ho Chi Minh City University of Technology +*/ + +#include"Node.h" + +Node::Node(int capacity) { + maxElements = capacity; + elements = new int[maxElements]; + numElements = 0; + prev = next = NULL; +} + +Node::~Node() { + if (elements != NULL) + delete[] elements; +} + +int Node::getHalfNodeSize() { + return (int)ceil(maxElements/2.0); +} + +bool Node::isUnderHalfFull() { + return numElements < getHalfNodeSize(); +} + +bool Node::isFull() { + return numElements >= maxElements; +} + +bool Node::isOverflow() { + return numElements > maxElements; +} + +bool Node::isEmpty() { + return numElements == 0; +} + +void Node::add(int val) { + if (isFull()) + throw "NodeOverflowExeception"; + else + { + elements[numElements] = val; + numElements++; + } + +} + +void Node::insertAt(int pos, int val) { + if (isFull()) throw "NodeOverflowExeception"; + else if (pos < 0 || pos > numElements) throw "IndexOutOfBoundsException"; + else { + memmove(elements + pos + 1, elements + pos, (numElements - pos)*sizeof(int)); + elements[pos] = val; + numElements++; + } +} + +void Node::removeAt(int pos) { + if (pos < 0 || pos >= numElements) throw "IndexOutOfBoundsException"; + else { + memmove(elements + pos, elements + pos + 1, (numElements - pos - 1)*sizeof(int)); + numElements--; + } +} + +void Node::reverse() { + for(int i = 0; i +// Unrolled Linked List Node + +class Node { +private: + int maxElements; +public: + int numElements; // number of elements in this node, up to maxElements + int* elements; // an array of numElements elements, + Node *next; // reference to the next node in the list + Node *prev; // reference to the previous node in the list + + Node(int capacity = 5); + ~Node(); + int getHalfNodeSize(); + bool isUnderHalfFull(); + bool isFull(); + bool isOverflow(); + bool isEmpty(); + void add(int val); // append val element to the end of the node + void insertAt(int pos, int val); // insert val to position pos of the node + void removeAt(int pos); // remove the position pos from the node + void reverse(); // reverse the content of the node + void print(); // print the content of the node + void printDetail(); // print the detail content of the node +}; + + + diff --git a/Assignment/UnrolledLinkedList.cpp b/Assignment/UnrolledLinkedList.cpp new file mode 100644 index 0000000..b25492a --- /dev/null +++ b/Assignment/UnrolledLinkedList.cpp @@ -0,0 +1,61 @@ +#include"UnrolledLinkedList.h" + +void UnrolledLinkedList::add(int val) { + // TODO + +} + +int UnrolledLinkedList::getAt(int pos) { + if (pos < 0 || pos >= size) + throw "IndexOutOfBoundsException"; // check whether pos is valid or not + // TODO + +} + +void UnrolledLinkedList::setAt(int pos, int val) { + if (pos < 0 || pos >= size) + throw "IndexOutOfBoundsException"; // check whether pos is valid or not + // TODO + +} + + +void UnrolledLinkedList::insertAt(int pos, int val) { + if (pos < 0 || pos > size) + throw "IndexOutOfBoundsException"; // check whether pos is valid or not + // TODO + +} + +void UnrolledLinkedList::deleteAt(int pos) +{ + if (pos < 0 || pos >= size) + throw "IndexOutOfBoundsException"; // check whether pos is valid or not + // TODO + +} + +int UnrolledLinkedList::firstIndexOf(int val) { + // TODO + +} + +int UnrolledLinkedList::lastIndexOf(int val) { + // TODO + +} + +bool UnrolledLinkedList::contains(int val) { + // TODO + +} + +void UnrolledLinkedList::reverse() { + // TODO + +} + +int* UnrolledLinkedList::toArray() { + // TODO + +} \ No newline at end of file diff --git a/Assignment/UnrolledLinkedList.h b/Assignment/UnrolledLinkedList.h new file mode 100644 index 0000000..fa03faf --- /dev/null +++ b/Assignment/UnrolledLinkedList.h @@ -0,0 +1,87 @@ +/* Copyright (C) 2018 +* Course: CO2003 +* Author: Rang Nguyen +* Ho Chi Minh City University of Technology +*/ + +#pragma once +#include"Node.h" + +class UnrolledLinkedList { +private: + Node* head; + Node* tail; + int size; + int numOfNodes; + int nodeSize; +public: + + // Your tasks: complete the implementation of the below methods in UnrolledLinkedList.h + void add(int val); + bool contains(int val); + int getAt(int pos); + void setAt(int pos, int val); + int firstIndexOf(int val); + int lastIndexOf(int val); + void insertAt(int pos, int val); + void deleteAt(int pos); + void reverse(); + int* toArray(); + + UnrolledLinkedList(int capacity) { + nodeSize = capacity; + head = tail = NULL; + size = numOfNodes = 0; + } + + ~UnrolledLinkedList() { + Node* p; + while (p = head) { + head = head->next; + delete p; + } + size = numOfNodes = 0; + head = tail = NULL; + } + + int getSize() { return size; } + int getHalfNodeSize() {return (int)ceil(nodeSize / 2.0);} + + void print() { + printf("=================METADATA================\n"); + printf("Size = %d, nodeSize = %d, halfNodeSize = %d\n", size, nodeSize, getHalfNodeSize()); + printf("-----------------CONTENTS----------------\n"); + Node* p = head; + if (p == NULL) { + printf("NULL\n"); + return; + } + + while (p != NULL) { + // check whether your implementation satisfied the requirements + if ((p->isUnderHalfFull() && size >= getHalfNodeSize()) || + p->isOverflow()) { + throw "Something wrong with your implementation"; + } + p->print(); + p = p->next; + + } + printf("\n-------------------END-------------------\n"); + } + + void printDetail() { + printf("=================METADATA================\n"); + printf("Size = %d, numOfNodes = %d, nodeSize = %d\n", size, numOfNodes, nodeSize); + printf("-----------------CONTENTS----------------\n"); + + Node* p = head; + if (p == NULL) printf("NULL\n"); + while (p != NULL) { + p->printDetail(); + p = p->next; + } + printf("-------------------END-------------------\n"); + } +}; + diff --git a/Assignment/input.txt b/Assignment/input.txt new file mode 100644 index 0000000..6534568 --- /dev/null +++ b/Assignment/input.txt @@ -0,0 +1,5 @@ +u 4 +a 5 +a 7 +i 0 1 +p diff --git a/Assignment/main.cpp b/Assignment/main.cpp new file mode 100644 index 0000000..9f89770 --- /dev/null +++ b/Assignment/main.cpp @@ -0,0 +1,95 @@ +/* Copyright (C) 2018 +* Course: CO2003 +* Author: Rang Nguyen +* Ho Chi Minh City University of Technology +*/ + +#include"UnrolledLinkedList.h" +#include +using namespace std; +int main() +{ + UnrolledLinkedList* list = NULL; + ifstream ifs; + ifs.open("input.txt"); + char command; + int pos, val; + try + { + while (ifs >> command) { + switch (command) { + case 'u': // create an object of UnrolledLinkedList + ifs >> val; + if (list != NULL) delete list; + list = new UnrolledLinkedList(val); + break; + case 'a': // append an element the the end of the list + ifs >> val; + list->add(val); + break; + case 'c': // check if an element is in the list or not + ifs >> val; + printf("The list contains %d: %d\n", val, list->contains(val)); + break; + case 'd': // delete an element at pos position in the list + ifs >> pos; + list->deleteAt(pos); + break; + case 'f': // return the index of the first occurence of a specific element in the list + ifs >> val; + printf("First occurrence of %d is at %d\n", val, list->firstIndexOf(val)); + break; + case 'g': // returns the element at the specified position in this list. + ifs >> pos; + printf("The element at position %d is %d\n", pos, list->getAt(pos)); + break; + case 'i': // insert a new element to pos position in the list + ifs >> pos >> val; + list->insertAt(pos, val); + break; + case 'l': // return the index of the last occurence of a specific element in the list + ifs >> val; + printf("Last occurrence of %d is at %d\n", val, list->lastIndexOf(val)); + break; + case 'p': // print the content of the list + list->print(); + break; + case 'r': // reverse the list + list->reverse(); + break; + case 's': // replaces the element at the specified position in this list with the specified element. + ifs >> pos >> val; + list->setAt(pos, val); + break; + case 't': //returns a pointer to an array containing all of the elements in this list in proper sequence(from first to last element). + { + int* arr = list->toArray(); + int n = list->getSize(); + printf("The list after converted to array:\n"); + if (n > 0) { + for (int i = 0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); + delete[] arr; + } + else { + printf("NULL\n"); + } + break; + } + case 'w': // print the detail content of the list + list->printDetail(); + break; + default: + throw "Wrong input format!"; + } + } + } + catch (char const* s) { + printf("An exception occurred. Exception type: %s\n", s); + } + + if (list != NULL) delete list; + ifs.close(); + return 0; +} \ No newline at end of file