#ifndef DOUBLY_LINKED_LIST #define DOUBLY_LINKED_LIST template class DLLNode { public: DLLNode() { next = prev = 0; } DLLNode(const T& el, DLLNode *n = 0, DLLNode *p = 0) { info = el; next = n; prev = p; } T info; DLLNode *next, *prev; }; template class DoublyLinkedList { public: DoublyLinkedList() { head = tail = 0; } void addToDLLTail(const T&); T deleteFromDLLTail(); ~DoublyLinkedList() { clear(); } bool isEmpty() const { return head == 0; } void clear(); void setToNull() { head = tail = 0; } void addToDLLHead(const T&); T deleteFromDLLHead(); T& firstEl(); T* find(const T&) const; protected: DLLNode *head, *tail; friend ostream& operator<<(ostream& out, const DoublyLinkedList& dll) { for (DLLNode *tmp = dll.head; tmp != 0; tmp = tmp->next) out << tmp->info << ' '; return out; } }; template void DoublyLinkedList::addToDLLHead(const T& el) { if (head != 0) { head = new DLLNode(el,head,0); head->next->prev = head; } else head = tail = new DLLNode(el); } template void DoublyLinkedList::addToDLLTail(const T& el) { if (tail != 0) { tail = new DLLNode(el,0,tail); tail->prev->next = tail; } else head = tail = new DLLNode(el); } template T DoublyLinkedList::deleteFromDLLHead() { T el = head->info; if (head == tail) { // if only one DLLNode on the list; delete head; head = tail = 0; } else { // if more than one DLLNode in the list; head = head->next; delete head->prev; head->prev = 0; } return el; } template T DoublyLinkedList::deleteFromDLLTail() { T el = tail->info; if (head == tail) { // if only one DLLNode on the list; delete head; head = tail = 0; } else { // if more than one DLLNode in the list; tail = tail->prev; delete tail->next; tail->next = 0; } return el; } template T* DoublyLinkedList::find(const T& el) const { DLLNode *tmp = head; for ( ; tmp != 0 && !(tmp->info == el); // overloaded == tmp = tmp->next); if (tmp == 0) return 0; else return &tmp->info; } template T& DoublyLinkedList::firstEl() { return head->info; } template void DoublyLinkedList::clear() { for (DLLNode *tmp; head != 0; ) { tmp = head; head = head->next; delete tmp; } } #endif