// Program to maintain a doubly linked list #include <iostream.h> class linklist { private : // structure representing a node of the doubly linked list struct dnode { dnode *prev ; int data ; dnode * next ; } *p ; public : linklist( ) ; void d_append ( int num ) ; void d_addatbeg ( int num ) ; void d_display( ) ; } ; // initializes data member linklist :: linklist( ) { p = NULL ; } // adds a new node at the end of the doubly linked list void linklist :: d_append ( int num ) { dnode *r, *q ; q = p ; // if the linked list is empty if ( q == NULL ) { //create a new node q = new dnode ; q -> prev = NULL ; q -> data = num ; q -> next = NULL ; p = q ; } else { // traverse the linked list till the last node is reached while ( q -> next != NULL ) q = q -> next ; // add a new node at the end r = new dnode ; r -> data = num ; r -> next = NULL ; r -> prev = q ; q -> next = r ; } } // adds a new node at the begining of the linked list void linklist :: d_addatbeg ( int num ) { dnode *q ; // create a new node q = new dnode ; // assign data and pointer to the new node q -> prev = NULL ; q -> data = num ; q -> next = p ; // make new node the head node p -> prev = q ; p = q ; } void linklist :: d_display( ) { dnode *temp = p ; cout << endl ; // traverse the entire linked list while ( temp != NULL ) { cout << temp -> data << " " ; temp = temp -> next ; } } } void main( ) { linklist l ; l.d_append ( 11 ) ; l.d_append ( 2 ) ; l.d_append ( 14 ) ; l.d_append ( 17 ) ; l.d_append ( 99 ) ; cout << "\nElements in doubly linked list: " ;