#pragma once #include #include #include "chainNode.h" #include "linearList.h" template class chain : public linearList { public: chain(int initialCapacity = 10); chain(const chain& x); ~chain(); virtual bool empty() const; //If empty, return 0; virtual int size() const; //Return Array Size virtual T& get(int theIndex) const; // Return Arr[theIndex] virtual int indexOf(const T& theElement) const; //Return the index of "theElement" first appeared virtual void erase(int theIndex); //Erase Arr[theIndex] virtual void insert(int theIndex, const T& theElement); //Insert theElement to place Arr[theIndex] virtual void output() const; //cout protected: chainNode* first; int listSize; int capacity; }; template chain::chain(int initialCapacity) { capacity = initialCapacity; } template chain::chain(const chain& x) { //**********Copy The WHOLE Chain********* capacity = x.capacity; listSize = x.listSize; //**********Deep Copy: Copy Every Node with figures******** if (x.first == nullptr) { first = nullptr; return; } chainNode* source = x.first; first = new chainNode(source->element);//equals to chainNode(source->element, nullptr) chainNode* target = first; source = source->next; while (source != nullptr) { target->next = new chainNode(source->element); target = target->next; source = source->next; } } template chain::~chain() { chainNode* tmp = first; while (tmp != nullptr) { tmp = tmp->next; delete first; first = tmp; } } template bool chain::empty() const { //If empty, return 0; return listSize == 0; } template int chain::size() const { //Return Array Size return listSize; } template T& chain::get(int theIndex) const { // Return Arr[theIndex] if (theIndex < 0 || theIndex >= listSize) { throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); } chainNode* tmp = first; for (int i = 1; i <= theIndex; i++) { tmp = tmp->next; } return tmp->element; } template int chain::indexOf(const T& theElement) const { //Return the index of "theElement" first appeared chainNode* tmp = first; for (int i = 0; i < listSize; i++) { if (tmp->element == theElement) { return i;//i = arr - 1 } tmp = tmp->next; } return -1; } template void chain::erase(int theIndex) { //Erase Arr[theIndex] if (theIndex < 0 || theIndex >= listSize) { throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); } chainNode* deleteNode; if (theIndex == 0) { deleteNode = first; first = first->next; } else { chainNode* prev = first; for (int i = 0; i < theIndex - 1; i++) { prev = prev->next; } deleteNode = prev->next; prev->next = deleteNode->next; } delete deleteNode; listSize--; } template void chain::insert(int theIndex, const T& theElement) { //Insert theElement to place Arr[theIndex] chainNode* arr = first; if (theIndex < 0) throw std::out_of_range("index out of range"); if (theIndex == 0) { // ²åÈ뵽ͷ²¿ first = new chainNode(theElement, first); } else { for (int i = 0; i < theIndex - 1; i++) { arr = arr->next; } chainNode* insert = new chainNode(theElement); insert->next = arr->next; arr->next = insert; listSize++; } } template void chain::output() const { //cout chainNode* tmp = first; for (int i = 0; i < listSize; i++) { std::cout << tmp->element << " "; tmp = tmp->next; } std::cout << std::endl; }