DoublyLinkedList.cpp

//Doubly LinkedList #include <cstdlib> #include <iostream> using namespace std; class DoublyNode { public: int data; DoublyNode *next; DoublyNode *prev; DoublyNode(int d, DoublyNode *ptr_n = 0, DoublyNode *ptr_p = 0) { data = d; next = ptr_n; prev = ptr_p; } }; class DoublyLinkedList { public: DoublyNode *head, *tail; DoublyLinkedList() { head = tail = 0; } ~DoublyLinkedList() { for(DoublyNode *p; !isEmpty();) { p = head->next; delete head; head = p; } } int isEmpty() { return head == 0; } void add(int data) { if (tail != 0) { tail->next = new DoublyNode(data, 0, tail); tail = tail->next; } else head = tail = new DoublyNode(data); } int size() { DoublyNode *p = head; int counter=0; while (p != 0) { p = p->next; counter++; } return counter; } void print() { DoublyNode *p = head; while (p != 0) { cout << p->data ; if(p->next != 0) cout << " <---> " ; p = p->next; } cout << "\n\n" ; } void printInReverse() { DoublyNode *p = tail; while (p != 0) { cout << p->data; if(p->prev != 0) cout << " <---> " ; p = p->prev; } cout << "\n\n" ; } }; int main() { DoublyLinkedList list; int const size1=5; int data1[size1]; list.add(100); list.add(27); list.add(13); list.add(4); list.print(); list.printInReverse(); return 0; }