#ifndef ___DOUBLE_LINKED_LIST__ #define ___DOUBLE_LINKED_LIST__ #include #include template class dnode { public: T data; dnode* prev, * next; dnode() { prev = next = NULL; } dnode(const T elem) { data = elem; prev = next = NULL; } }; template class Itr { private: dnode* current; public: Itr() { current = NULL; } Itr(dnode* ptr) { current = ptr; } Itr& operator++(); //pre-increment ++i Itr operator++(int); //post-increment i++ Itr& operator--(); //pre-decrement --i Itr operator--(int); //post-decrement i-- T operator*() const { return current->data; }; T& operator*() { return current->data; }; const dnode* operator->() const { return current; }; dnode*& operator->() { return current; }; bool operator==(const Itr itr2) { if (itr2 == NULL) { if (current == NULL) return true; else return false; } if (currnt == itr2.current) return true; else return false; } bool operator!=(Itr itr2) { return !(*this == itr2); } }; template Itr& Itr::operator++() //pre-increment ++i { if (current != NULL) current = current->next; //Itr rlt(current); //return rlt; return *this; } template Itr Itr::operator++(int) //post-increment i++ { Itr rlt(current); if (current != NULL) current = current->next; return rlt; } template Itr& Itr::operator--() //pre-decrement --i { if (current != NULL) current = current->prev; //Itr rlt(current); //return rlt; return *this; } template Itr Itr::operator--(int) //post-decrement i-- { Itr rlt(current); if (current != NULL) current = current->prev; return rlt; } template class DList { private: dnode* front, * rear; public: DList() { front = rear = NULL; } ~DList() { dnode* temp; while (front != NULL) { temp = front; front = front->next; delete temp; } } bool isEmpty() const { return (front == NULL); } bool isFull() const { dnode* temp = new dnode; if (temp != NULL) { delete temp; return false; } else return true; } const Itr operator[](int) const; Itr& operator[](int); const Itr find(const T &elem) const; Itr& find(const T & elem); const Itr begin() const { return Itr(front); } Itr& begin() { Itr* i = new Itr(front); return *i; } // Itr& begin() { return Itr(front); } const Itr end() const { return Itr(rear); } Itr& end() { Itr* i = new Itr(rear); return *i; } // Itr& end() { return Itr(rear); } void insertBeforeNode(const T& elem, Itr iterator); void insertAfterNode(const T& elem, Itr iterator); void remove(Itr iterator); }; template const Itr DList::operator[](int n) const { assert(n >= 0); dnode* tmp = front; for (int i = 0; i < n; i++) { if (tmp == NULL) break; tmp = tmp->next; } return Itr(tmp); } template Itr& DList::operator[](int n) { assert(n >= 0); dnode* tmp = front; for (int i = 0; i < n; i++) { if (tmp == NULL) break; tmp = tmp->next; } Itr* iter = new Iter(tmp); return *iter; } template const Itr DList::find(const T& elem) { dnode* tmp = front; while (tmp != NULL && tmp->data != elem) tmp = tmp->next; return Itr(tmp); } template void DList::insertBeforeNode(const T& elem, Itr iterator) { dnode* newNode = new dnode(elem); // list is empty if (front == NULL) { front = rear = newNode; return; } // iterator is pointing to front of the list if (iterator.operator->() == front) { } #endif