diff --git a/OOP/12/Excercise01.cpp b/OOP/12/Excercise01.cpp new file mode 100644 index 0000000..6e0fe05 --- /dev/null +++ b/OOP/12/Excercise01.cpp @@ -0,0 +1,58 @@ +#include +using std::cout; +using std::endl; + +template +void swap(T list[], const int left, const int right) { + T tmp = list[left]; + list[left] = list[right]; + list[right] = tmp; +} + +template +T *sort(T originList[], const int left, const int right) { + int l = left, r = right; + int pivot = left; + if (left >= right) { + return originList; + } + while (l < r) { + while (originList[r] >= originList[pivot] && r > l) { + r--; + } + swap(originList, pivot, r); + pivot = r; + while (originList[l] <= originList[pivot] && l < r) { + l++; + } + swap(originList, pivot, l); + pivot = l; + } + sort(originList, left, pivot - 1); + sort(originList, pivot + 1, right); + return originList; +} + +int main() { + int intList[6] = {1, 6, 4, 5, 2, 3}; + char charList[14] = "Muelsyse.com!"; + for (int i = 0; i < 6; i++) { + cout << intList[i] << ' '; + } + cout << endl; + for (int i = 0; i < 13; i++) { + cout << charList[i] << ' '; + } + cout << endl; + sort(intList, 0, 5); + sort(charList, 0, 9); + for (int i = 0; i < 6; i++) { + cout << intList[i] << ' '; + } + cout << endl; + for (int i = 0; i < 13; i++) { + cout << charList[i] << ' '; + } + cout << endl; + return 0; +} \ No newline at end of file diff --git a/OOP/12/Exercise02.cpp b/OOP/12/Exercise02.cpp new file mode 100644 index 0000000..f60cf55 --- /dev/null +++ b/OOP/12/Exercise02.cpp @@ -0,0 +1,96 @@ +#include +using std::cout; +using std::endl; + +template class Storage { +private: + E *objectEptr; + int length; + void swap(int left, int right) { + E tmp = objectEptr[left]; + objectEptr[left] = objectEptr[right]; + objectEptr[right] = tmp; + } + void quickSort(const int left, const int right) { + if (left >= right) { + return; + } + int l = left, r = right; + int pivot = left; + while (l < r) { + while (objectEptr[r] >= objectEptr[pivot] && r > l) { + r--; + } + this->swap(pivot, r); + pivot = r; + while (objectEptr[l] <= objectEptr[pivot] && l < r) { + l++; + } + this->swap(pivot, l); + pivot = l; + } + this->quickSort(left, pivot - 1); + this->quickSort(pivot + 1, right); + } + +public: + Storage() : objectEptr(NULL){}; + Storage(const int + _length) : objectEptr(new E[_length]), length(_length){}; + Storage(const E *originptr, const int _n) + : objectEptr(new E[_n]), length(_n) { + for (int i = 0; i < _n; i++) { + objectEptr[i] = originptr[i]; + } + }; + ~Storage() { + delete[] objectEptr; + } + Storage &sort() { + quickSort(0, length - 1); + return *this; + } + Storage &sort(const int leftIndex, const int rightIndex) { + quickSort(leftIndex, rightIndex); + return *this; + } + E *getList() { + return this->objectEptr; + } + int getLength() { + return this->length; + } + Storage &printOut() { + for (int i = 0; i < this->length; i++) { + cout << objectEptr[i] << ' '; + } + cout << endl; + return *this; + } + E &operator[](int index) { + return objectEptr[index]; + } + + int search(E target) { + for (int i = 0; i < this->length; i++) { + if (target == objectEptr[i]) { + return i; + } + } + return -1; + } +}; + +int main() { + int list[6] = {1, 4, 6, 5, 3, 2}; + std::string str = "HelloWorld!"; + Storage aList(list, 6); + Storage charList(str.c_str(), str.length() + 1); + aList.printOut().sort().printOut(); + charList.printOut().sort(0, charList.getLength() - 2).printOut(); + aList[2] = 8; + aList.printOut(); + cout << "8 is at index: " << aList.search(8) << endl; + cout << "H is at index: " << charList.search('H') << endl; + return 0; +} \ No newline at end of file diff --git a/OOP/12/Optional01/ListExceptions.h b/OOP/12/Optional01/ListExceptions.h new file mode 100644 index 0000000..dc9ed17 --- /dev/null +++ b/OOP/12/Optional01/ListExceptions.h @@ -0,0 +1,8 @@ +#pragma once +class BaseListExceptions {}; + +class ValueError : public BaseListExceptions {}; + +class IndexError : public BaseListExceptions {}; + +class DuplicateError : public BaseListExceptions {}; \ No newline at end of file diff --git a/OOP/12/Optional01/listE.h b/OOP/12/Optional01/listE.h new file mode 100755 index 0000000..70b2f5c --- /dev/null +++ b/OOP/12/Optional01/listE.h @@ -0,0 +1,163 @@ +#pragma once +#include "ListExceptions.h" +#include "node.h" + +template class List { +private: + Node *head; + int _length; + int getIndexInRange(int index); + +public: + List(); + List(const E &newItem); + List(const E *newItemList, const int itemCount); + ~List(); + // Append _newItem at last. Returns the List itself. + List &append(const E &_newItem); + // Insert _newItem at index, and move all items at and after [index] 1 step. + // Throws IndexError if index is not in the range of [-_length, _length - + // 1]. Returns the List itself. + List &insert(int index, const E &_newItem); + // Pop the item at given index, default is the to pop the last one. + E pop(int index = -1); + // Remove the first occurance of target. Throws ValueError if target does + // not exist. Returns the List itself. + List &remove(const E &target); + // Clear all nodes. + List &clear(); + E &operator[](const int index); + + int length(); + bool contains(const E &target); + // Give the index of target in the list on its first appearance. Throws + // ValueError if target does not exist. + int index(const E &target); +}; + +template int List::getIndexInRange(int index) { + if (index < 0) { + index += this->_length; + } + if (index < 0 || index >= _length) { + throw(IndexError()); + } + return index; +} + +template List::List() : head(NULL), _length(0){}; + +template +List::List(const E &newItem) : head(new Node(newItem)), _length(1){}; + +template List::List(const E *newItemList, const int itemCount) { + for (int i = 0; i < itemCount; i++) { + this->append(newItemList[i]); + } +}; + +template List::~List() { + if (this->head != NULL) { + this->head->destruct(); + } + head = NULL; +} + +template List &List::append(const E &_newItem) { + if (this->head == NULL) { + this->head = new Node(_newItem); + } + else { + this->head->getTail().setNext(new Node(_newItem)); + } + this->_length++; + return *this; +} + +template List &List::insert(int index, const E &_newItem) { + index = this->getIndexInRange(index); + if (index == 0) { + this->head = new Node(_newItem, this->head); + } + else { + Node &prev = this->head->getByIndex(index - 1); + prev.setNext(new Node(_newItem, prev.getNextPtr())); + } + this->_length++; + return *this; +} + +template List &List::remove(const E &target) { + this->pop(this->index(target)); + return *this; +} + +template E List::pop(int index) { + if (_length == 0) { + throw(IndexError()); + } + + index = this->getIndexInRange(index); + + Node removed = this->head->getByIndex(index); + + this->_length--; + if (index == 0) { + Node *newHead = this->head->getNextPtr(); + delete this->head; + this->head = newHead; + } + else { + Node &prev = this->head->getByIndex(index - 1); + delete prev.getNextPtr(); + prev.setNext(removed.getNextPtr()); + } + return removed.getContent(); +} + +template List &List::clear() { + this->head->destruct(); + this->head = NULL; + this->_length = 0; + return *this; +} + +template E &List::operator[](const int index) { + return this->head->getByIndex(getIndexInRange(index)).getContent(); +} + +template int List::length() { + return this->_length; +} + +template bool List::contains(const E &target) { + if (this->_length == 0) { + return false; + } + + Node *current = this->head; + do { + if (current->getContent() == target) { + return true; + } + current = current->getNextPtr(); + } while (current != NULL); + return false; +} + +template int List::index(const E &target) { + if (this->_length == 0) { + throw(ValueError()); + } + + Node *current = this->head; + int pos = 0; + do { + if (current->getContent() == target) { + return pos; + } + current = current->getNextPtr(); + pos++; + } while (current != NULL); + throw(ValueError()); +} \ No newline at end of file diff --git a/OOP/12/Optional01/main.cpp b/OOP/12/Optional01/main.cpp new file mode 100755 index 0000000..e80b671 --- /dev/null +++ b/OOP/12/Optional01/main.cpp @@ -0,0 +1,65 @@ +#include "listE.h" +#include "setE.h" +#include +using std::cout; +using std::endl; + +template void output(List& list) { + for (int i = 0; i < list.length(); i++) { + cout << list[i] << ' '; + } + if (list.length() == 0) { + cout << "(Empty)"; + } + cout << endl; +} + +int main() { + int nums[5] = {1, 4, 6, 8, 3}; + List aList; + List bList(nums, 5); + output(bList); + try { + aList.remove(0); + } + catch(ValueError) { + std::cerr << "There is an ValueError" << std::endl; + } + aList.append(3); + aList.append(8); + aList.append(9); + aList.append(12); + aList.insert(1, 20); + output(aList); + + cout << aList.pop() << endl; + output(aList); + + aList.pop(2); + output(aList); + + cout << aList.contains(3) << ' ' << aList.contains(15) << endl; + + try { + aList.index(15); + } + catch(ValueError) { + std::cerr << "There's no 15 in this list." << std::endl; + } + + aList.clear(); + output(aList); + + cout << "\n----------\n" << endl; + + Set aSet; + aSet.show(); + + aSet.add(2); + aSet.add(3); + aSet.show(); + + aSet.remove(2); + aSet.show(); + return 0; +} \ No newline at end of file diff --git a/OOP/12/Optional01/node.h b/OOP/12/Optional01/node.h new file mode 100755 index 0000000..bdcf4a6 --- /dev/null +++ b/OOP/12/Optional01/node.h @@ -0,0 +1,67 @@ +#pragma once +#include + +template class Node { +private: + E content; + Node *nextNode; + +public: + Node(); + Node(E _content, Node *_nextNode = NULL); + ~Node(); + // Returns a reference to the content. + E &getContent(); + // Returns a reference to the next Node. May return NULL if this Node is the + // last one. + Node *getNextPtr(); + // Set the next Node _nextNode. Returns THIS Node. + Node &setNext(Node *_nextNode); + // Returns a reference to the Tail. + Node &getTail(); + // Returns a reference to the Node of given index. + Node &getByIndex(const int index); + void destruct(); +}; + +template Node::Node() : content(0), nextNode(NULL){}; + +template +Node::Node(E _content, Node *_nextNode) + : content(_content), nextNode(_nextNode){}; + +template Node::~Node(){}; + +template E &Node::getContent() { + return this->content; +} + +template Node *Node::getNextPtr() { + return this->nextNode; +} + +template Node &Node::setNext(Node *_nextNode) { + this->nextNode = _nextNode; + return *this; +} + +template Node &Node::getTail() { + if (this->nextNode == NULL) { + return *this; + } + return this->nextNode->getTail(); +} + +template Node &Node::getByIndex(const int index) { + if (index == 0) { + return *this; + } + return this->nextNode->getByIndex(index - 1); +} + +template void Node::destruct() { + if (this->nextNode != NULL) { + this->nextNode->destruct(); + } + delete this; +} \ No newline at end of file diff --git a/OOP/12/Optional01/setE.h b/OOP/12/Optional01/setE.h new file mode 100644 index 0000000..bc49859 --- /dev/null +++ b/OOP/12/Optional01/setE.h @@ -0,0 +1,69 @@ +#include "ListExceptions.h" +#include "listE.h" + +template class Set { +private: + List _list; + +public: + Set(); + Set(const E &_newItem); + Set(const E *_newItem, const int count); + // Add an element. + Set &add(const E &_newItem); + // Remove an element. + Set &remove(const E &target); + // Clear all elements. + Set &clear(); + + int size(); + void show(); +}; + +template Set::Set() : _list(){}; + +template Set::Set(const E &_newItem) : _list(_newItem){}; + +template Set::Set(const E *_newItem, const int count) : _list() { + for (int i = 0; i < count; i++) { + try { + this->add(_newItem[i]); + } + catch (DuplicateError) { + continue; + } + } +}; + +template Set &Set::add(const E &_newItem) { + if (_list.contains(_newItem)) { + throw(DuplicateError()); + } + + _list.append(_newItem); + return *this; +}; + +template Set &Set::remove(const E &target) { + _list.remove(target); + return *this; +} + +template Set &Set::clear() { + _list.clear(); + return *this; +} + +template int Set::size() { + return _list.length(); +} + +template void Set::show() { + for (int i = 0; i < _list.length(); i++) { + std::cout << _list[i] << ' '; + } + if (_list.length() == 0) { + std::cout << "(Empty)"; + } + std::cout << std::endl; +} \ No newline at end of file diff --git a/OOP/test.cpp b/OOP/test.cpp index 975311e..d3a898f 100644 --- a/OOP/test.cpp +++ b/OOP/test.cpp @@ -59,6 +59,12 @@ public: int main() { // D objD(1, 2, 3, 4); // objD.display(); - cout << "---\n" << sizeof(A) << endl << sizeof(B) << endl << sizeof(C) << endl; + // cout << "---\n" << sizeof(A) << endl << sizeof(B) << endl << sizeof(C) << endl; + // cout << -13 % 6 << endl; + cout << "---"; + endl(cout); + endl(cout); + cout << "---"; + endl(cout); return 0; } \ No newline at end of file diff --git a/POP/13/Optional01.c b/POP/13/Optional01.c index cc1f8cc..8057360 100644 --- a/POP/13/Optional01.c +++ b/POP/13/Optional01.c @@ -60,7 +60,7 @@ NODE* insert(NODE* head, int index, int value) { NODE* newNodePtr; currentNode = head; if ((*currentNode).index > index) { - newNodePtr = malloc(LEN); + newNodePtr = (NODE*) malloc(LEN); (*newNodePtr).index = index; (*newNodePtr).value = value; (*newNodePtr).prev = NULL; @@ -77,7 +77,7 @@ NODE* insert(NODE* head, int index, int value) { currentNode = (*currentNode).next; continue; } - newNodePtr = malloc(LEN); + newNodePtr = (NODE*)malloc(LEN); (*newNodePtr).index = index; (*newNodePtr).value = value; (*newNodePtr).prev = currentNode; @@ -86,7 +86,7 @@ NODE* insert(NODE* head, int index, int value) { (*currentNode).next = newNodePtr; return newNodePtr; } - newNodePtr = malloc(LEN); + newNodePtr = (NODE*)malloc(LEN); (*newNodePtr).index = index; (*newNodePtr).value = value; (*newNodePtr).prev = currentNode; @@ -97,7 +97,7 @@ NODE* insert(NODE* head, int index, int value) { NODE* create(int index, int value) { NODE* newNodePtr; - newNodePtr = malloc(LEN); + newNodePtr = (NODE*)malloc(LEN); (*newNodePtr).index = index; (*newNodePtr).value = value; (*newNodePtr).prev = NULL;