/************ 3. A Doubly Linked List is a data structure consisting of nodes that have a pointer to the next object in the list as well as a pointer to the previous object. class A{ public: .......... //Data A* next; A* previous; ..... } Write a program to create a doubly linked list of nodes that hold an int and a char values as data. Include functions to print the list backward and forward, as well as to add items to the list and remove from the list. **************/ #include using namespace std; class A{ public: int a; char c; A* next; A* previous; A(){next=NULL;previous=NULL;} A(int i=0, char x='a'){ a=i; c=x; next=NULL; previous=NULL; } void add_to_tail(A* p){ A* temp=this; if(next==NULL){ next=p; p->previous=temp; } else{ while((temp->next)!=NULL){ temp=temp->next; } p->previous=temp; temp->next=p; } } A* add_to_head(A* p){ p->next=this; previous=p; return p; } void display(){ A* p=this; while(p!= NULL){ cout<a<<"-->\t"<c<next; } } }; int main(){ A a1(2,'a'),a2(4,'A'),a3(6,'C'),a4(8,'J'),a5(5,'k'); A* head=&a1; head->display(); cout<<"****adding to tail****"<add_to_tail(&a2); head->display(); cout<<"****adding to tail****"<add_to_tail(&a4); head->display(); cout<<"****adding to tail****"<add_to_tail(&a5); head->display(); cout<<"****adding to head****"<add_to_head(&a3); head->display(); }