Write a C++ Program to Implementation of Doubly linked list

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NODE
{
public:
     int data;
     NODE *next,*prev;
     NODE *create();
     void display(NODE*);
     void insert_e(NODE*);
     void sort(NODE*);
     NODE *merge(NODE*,NODE*);
};
NODE *nw,*temp,*head,*t1,*pre,*p,*end,*head1,*head2,*r,*q,*pt1,*pt2;
NODE* NODE::create()
{
     head=new(NODE);
     head->data=0;
     head->next=NULL;
     head->prev=NULL;
     return(head);
}
void NODE::insert_e(NODE* head)
{
     nw=new(NODE);
     cin>>nw->data;
     cout<<"\nNode is entered successfully\n\n";
     nw->next=NULL;
     temp=head;
     while(temp->next!=NULL)
           temp=temp->next;
     temp->next=nw;
     nw->prev=temp;
     head->data++;
}

void NODE::sort(NODE* head)
{
     end=NULL;
     while(head->next!=end)
     {
           pre=head;
           temp=pre->next;
           while(temp->next!=end)
           {
                p=temp->next;
                if(temp->data>p->data)
                {
                     pre->next=p;
                     temp->next=p->next;
                     p->next=temp;
                     temp->prev=p;
                }
                else
                     temp=temp->next;
                pre=pre->next;
           }

           end=temp;
     }
}
void NODE::display(NODE* head)
{
     temp=head->next;
     while(temp!=NULL)
     {
           cout<<temp->data;
           temp=temp->next;
           cout<<"->";
     }
     cout<<"NULL\n";
}
NODE* NODE::merge(NODE *p,NODE *q)
{
     sort(p);
     sort(q);
     if(p->next->data<q->next->data)
     {
           r=p;
           pre=p->next;
           pt1=pre->next;
           pt2=q->next;
     }
     else
     {
           r=q;
           pre=q->next;
           pt2=pre->next;
           pt1=p->next;
     }
     while(pt1!=NULL&&pt2!=NULL)
     {
           while(pt1->data<=pt2->data&&pt1!=NULL)
           {
                pre->next=pt1;
                pt1->prev=pre;
                pt1=pt1->next;
                pre=pre->next;
           }
           while(pt2->data<=pt1->data&&pt2!=NULL)
           {
                pre->next=pt2;
                pt2->prev=pre;
                pt2=pt2->next;
                pre=pre->next;
           }
     }
     if(pt1!=NULL)
     {
           pre->next=pt1;
           pt1->prev=pre;
     }
     else
     {
           pre->next=pt2;
           pt2->prev=pre;
     }
     return(r);
}
void main()
{
     int no,data;
     NODE a;
     clrscr();
     do
     {
           cout<<"\n\tMENU\n1.Create heads\n2.Insert at end of list        1\n3.Insert at end of list 2\n4.Display lists\n5.Sort                lists\n6.Merge\n7.Exit\n";
           cout<<"Enter your choice:";
           cin>>no;
           switch(no)
           {
                case 1:
                      head1=a.create();
                     head2=a.create();
                     cout<<"\nHeads created";
                     break;
                case 2:
                     cout<<"\nInserting at end in list 1\nInsert data:";
                     a.insert_e(head1);
                     break;
                case 3:
                     cout<<"\nInserting at end in list 2\nInsert data:";
                     a.insert_e(head2);
                     break;
                case 4:
                     a.display(head1);
                     a.display(head2);
                     break;
                case 5:
                     cout<<"\nSorted nodes are:";
                     a.sort(head1);
                     a.sort(head2);
                     a.display(head1);
                     a.display(head2);
                     break;
                case 6:
                     head=a.merge(head1,head2);
                     a.display(head);
                     break;

                case 7:
                     exit(0);
                     break;
                default:
                     cout<<"PLEASE ENTER CORRECT CHOICE";
                     break;
           }
     }while(no<7);
     getch();
}




-----------------------------Output-------------------------------

        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:1

Heads created
        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:2

Inserting at end in list 1
Insert data:3

Node is entered successfully


        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:3

Inserting at end in list 2
Insert data:7

Node is entered successfully


        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:4
3->NULL
7->NULL

        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:5

Sorted nodes are:3->NULL
7->NULL

        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit
Enter your choice:6
3->7->NULL

        MENU
1.Create heads
2.Insert at end of list 1
3.Insert at end of list 2
4.Display lists
5.Sort lists
6.Merge
7.Exit

Enter your choice:7
Previous
Next Post »

Disqus Shortname

Comments system