#include<iostream.h>
#include<process.h>
#include<conio.h>
class dll
{
private:
                int data;
                dll *prev,*next;
public:
                dll()
                {
                                data=0;
                                prev=next=NULL;
                }
                dll(int data_in)
                {
                                data=data_in;
                                prev=next=NULL;
                }
                ~dll()
                {
                                cout<<" -> "<<data;
                }
                int get()
                {
                                return data;
                }
                void insert(dll *node);
                friend void display(dll *);
                void freeallnodes();
};
void dll::insert(dll *node)
{
                dll *last;
                for(last=this;last->next;last=last->next);
                node->prev=last;
                node->next=last->next;
                last->next=node;
}
void dll::freeallnodes()
{
                cout<<"\n\tFreeing the node with data ";
                for(dll *first=this;first;first=first->next)
                                delete first;
}
void display(dll *first)
{
                dll *traverse=first;
                if(traverse==NULL)
                                {
                                cout<<"\nNothing to Display ! "<<endl;
                                return;
                                }
                else
                                {
                                cout<<"Processing with forward -> pointer:- ";
                                for(;traverse->next;traverse=traverse->next)
                                                cout<<" -> "<<traverse->data;
                                cout<<" -> "<<traverse->data<<endl;
                                cout<<"Porcessing with backward <- pointer:- ";
                                for(;traverse->prev;traverse=traverse->prev)
                                                cout<<" -> "<<traverse->data;
                                cout<<" -> "<<traverse->data<<endl;
                                }
}
dll *insertnode(dll *first,int data)
{
                dll *node;
                node=new dll(data);
                if(first==NULL)
                                first=node;
                else
                                first->insert(node);
                return first;
}
void main(void)
{
                int choice,data;
                dll *first=NULL;
                clrscr();
                cout<<"Doubly Linked List Manipulation _ _ _ _ _ "<<endl;
                while(1)
                                {
                                cout<<"\n[1] Insert\n[2] Display\n[3] Quit\nEnter Choice:- ";
                                cin>>choice;
                                switch(choice)
                                                {
                                                case 1:
                                                                cout<<"Enter Data:- ";
                                                                cin>>data;
                                                                first=insertnode(first,data);
                                                                break;
                                                case 2:
                                                                display(first);
                                                                break;
                                                case 3:
                                                                first->freeallnodes();
                                                                getch();
                                                                exit(1);
                                                default:
                                                                cout<<"\n\tBad Option Selected"<<endl;
                                                continue;
                                                }
                                }
}
Advertisements