Thursday, August 29, 2013

B. IMPLEMENTATION OF DLL


/* Doubly Linked List  ( DLL ) */

/**********************************************
              NAME                :-  JABIR DAUD PATHAN
PROGRAM    :-  IMPLEMENTATION OF DLL
**********************************************/

/*------------<<  Header File  >>------------*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

 struct node
   {
     int data;
     node*next;
     node*prev;
   };

/*-------<<  Declaration of class dll >>--------*/

class dll
 {
  private:
               node*head;
  public :
               dll()  // Default constructor
                {
                  head=NULL;
                }
               void create();             // [ Functions
               void display();            //   declaraton ]
               void insert_beginning();
               void insert_between();
               void insert_end();
               void delete_beginning();
               void delete_between();
               void delete_end();
               void search();
               void reverse();
 };

/*-------<<  Function defination for create dll >>-------*/

void dll :: create()
{
 node *t,*curr;
 int n,i;
 cout<<"\nEnter the no of nodes :";
 cin>>n;
 for(i=0;i<n;i++)
  {
   t=new node;
   t->prev=NULL;
   t->next=NULL;
   cout<<"\n\nEnter values of node==>";
   cin>>t->data;
   if(head==NULL)
    {
     head=t;
     curr=t;
    }
   else
    {
     curr->next=t;
     t->prev=curr;
     curr=curr->next;
    }
  }
}

/*-------<<  Function defination for display dll >>-------*/

void dll :: display()
 {
  node *temp;
  temp=head;
  if(head==NULL)
   {
    cout<<"list is empty";
   }
  else
   {
    cout<<"\n\nCreated link list : ";
    while(temp!=NULL)
     {
      cout<<"<--"<<temp->data<<"-->";
      temp=temp->next;
     }
   }
 }

/*-----<<  Function defination for search any node >>------*/

void dll :: search()
 {
  node *p;
  int m,cnt=1;
  cout<<"\n\nEnter the data to be searched: ";
  cin>>m;
  for(p=head;p!=NULL;p=p->next)
   {
    if(p->data==m)
      {
       cout<<"\nFound at location "<<cnt;
       break;
      }
    cnt++;
   }
  if(p->data!=m)
   {
    cout<<"\nNot Found";
   }
 }

/*-----<<  Function defination for reversing the dll >>------*/

void dll :: reverse()
 {
  node*p;
  p=head;
  while(p->next!=NULL)
   {
    p=p->next;
   }
  cout<<"\n\nCreated link list : ";
  while(p!=NULL)
   {
    cout<<"<--"<<p->data<<"-->";
    p=p->prev;
   }
 }

/*----<<  Function defination for inserting node at beginning >>----*/

void dll :: insert_beginning()
 {
  node *p;
  int j;
  cout<<"\nEnter the value to be inserted==>";
  cin>>j;
  p=new node;
  p->data=j;
  p->prev=NULL;
  p->next=head;
  head=p;
 }

/*----<<  Function defination for inserting node in between >>----*/

void dll :: insert_between()
 {
  node *p,*k;
  int m,n;
  cout<<"\nEnter the value to be inserted==>";
  cin>>m;
  p=new node;
  p->data=m;
  p->prev=NULL;
  p->next=NULL;
  cout<<"\n\nEnter the previous node==>";
  cin>>n;
  k=head;
  while(k!=NULL && k->data!=n)
   {
    k=k->next;
   }
  p->next=k->next;
  p->prev=k;
  k->next=p;
 }

/*----<<  Function defination for inserting node at end >>-----*/

void dll :: insert_end()
 {
  node *p,*k;
  int j;
  cout<<"\nEnter the value to be inserted==>";
  cin>>j;
  p=new node;
  p->data=j;
  p->prev=NULL;
  p->next=NULL;
  k=head;
  while(k->next!=NULL)
   {
    k=k->next;
   }
  p->prev=k;
  k->next=p;
 }

/*---<<  Function defination for deleting beginning node >>-----*/

void dll :: delete_beginning()
 {
  node *p;
  p=head;
  p->next->prev=NULL;
  head=p->next;
  delete p;
 }

/*---<<  Function defination for deleting any node from between >>---*/

void dll :: delete_between()
 {
  node *p;
  int x;
  cout<<"\nEnter the data to be deleted : ";
  cin>>x;
  p=head;
  while(p->data!=x && p->next !=NULL)
   {
    p=p->next;
   }
   p->prev->next=p->next;
   p->next->prev=p->prev;
   delete p;
  }

/*-----<<  Function defination for deleting end node >>------*/

void dll :: delete_end()
 {
  node *p;
  p=head;
  while(p->next!=NULL)
   p=p->next;
  p->prev->next=NULL;
  delete p;
 }

/*-------------<<  Main Function Starts  >>------------*/

void main()
 {
   dll j;
   char ans;
   int ch;
do
 {
  clrscr();
  cout<<"\n\t\t\t---<< Operations on DLL >>---";
  cout<<"\n\n\t\t\t\t1.Create";
  cout<<"\n\t\t\t\t2.Display";
  cout<<"\n\t\t\t\t3.Insert";
  cout<<"\n\t\t\t\t4.Delete";
  cout<<"\n\t\t\t\t5.Search";
  cout<<"\n\t\t\t\t6.Reverse";
  cout<<"\n\t\t\t\t7.Exit";
  cout<<"\n\n\t\t\t-----------------------------";

  cout<<"\n\n\t\t\tEnter your choice : ";
  cin>>ch;

  switch(ch)
   {
    case 1:
                   j.create();
                   break;
    case 2:
                   j.display();
                   break;
    case 3:
                   j.display();
                   do
                        {
                         cout<<"\n\n\tInsert:";
                         cout<<"\n\t\t1.Beginning";
                         cout<<"\n\t\t2.In Between";
                         cout<<"\n\t\t3.End";
                         cout<<"\n\n\t\tEnter your choice : ";
                         cin>>ch;
                         switch(ch)
                             {
                              case 1:
                                                   j.insert_beginning();
                                                   j.display();
                                                   break;

                              case 2:
                                                   j.insert_between();
                                                   j.display();
                                                   break;

                              case 3:
                                                   j.insert_end();
                                                   j.display();
                                                   break;
                           }
                             cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
                             ans=getche();
                         }while(ans=='Y'||ans=='y');
                          break;
     case 4:
                   j.display();
                   do
                        {
                         cout<<"\n\n\tDelete:";
                         cout<<"\n\t\t1.Beginning";
                         cout<<"\n\t\t2.In Between";
                         cout<<"\n\t\t3.End";
                         cout<<"\n\n\t\tEnter your choice : ";
                         cin>>ch;
                         switch(ch)
                             {
                              case 1:
                                                   j.delete_beginning();
                                                   j.display();
                                                   break;

                              case 2:
                                                   j.delete_between();
                                                   j.display();
                                                   break;

                              case 3:
                                                   j.delete_end();
                                                   j.display();
                                                   break;
                             }
                             cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
                             ans=getche();
                         }while(ans=='Y'||ans=='y');
                          break;



    case 5:
                   j.display();
                   j.search();
                   break;
    case 6:
                   j.display();
                   j.reverse();
                   break;

    case 7:
                   exit(0);
                   break;

   }
   cout<<"\n\n\n\t\tDo you want to continue(Y/N)? :";
   ans=getche();
  }while(ans=='Y'||ans=='y');
   getch();
 }

/*-------------<<  End Of Main Function  >>---------------*/






/*-----------------<< OUTPUT SCREEN  >>-------------------*/

                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 1

Enter the no of nodes :3

Enter values of node==>7

Enter values of node==>8

Enter values of node==>9

                Do you want to continue(Y/N)? :Y

                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 2

Created link list : <--7--><--8--><--9-->

                Do you want to continue(Y/N)? :Y



                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 3


Created link list : <--7--><--8--><--9-->


        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 1

Enter the value to be inserted==>10

Created link list : <--10--><--7--><--8--><--9-->

                Do you want to continue(Y/N)? :Y

        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 2

Enter the value to be inserted==>11

Enter the previous node==>8

Created link list : <--10--><--7--><--8--><--11--><--9-->


                Do you want to continue(Y/N)? :Y

        Insert:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 3

Enter the value to be inserted==>12

Created link list : <--10--><--7--><--8--><--11--><--9--><--12-->

                Do you want to continue(Y/N)? :N

                Do you want to continue(Y/N)? :Y


                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 4


Created link list : <--10--><--7--><--8--><--11--><--9--><--12-->

        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 1


Created link list : <--7--><--8--><--11--><--9--><--12-->


                Do you want to continue(Y/N)? :Y

        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 2

Enter the data to be deleted : 8

Created link list : <--7--><--11--><--9--><--12-->


                Do you want to continue(Y/N)? :Y

        Delete:
                1.Beginning
                2.In Between
                3.End

                Enter your choice : 3


Created link list : <--7--><--11--><--9-->


                Do you want to continue(Y/N)? :N

                Do you want to continue(Y/N)? :Y

                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 5

Created link list : <--7--><--11--><--9-->

Enter the data to be searched: 7

Found at location 1


                Do you want to continue(Y/N)? :Y


                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 6


Created link list : <--7--><--11--><--9-->

Created link list : <--9--><--11--><--7-->


                Do you want to continue(Y/N)? :Y

                        ---<< Operations on DLL >>---

                                1.Create
                                2.Display
                                3.Insert
                                4.Delete
                                5.Search
                                6.Reverse
                                7.Exit

                        -----------------------------

                        Enter your choice : 7

/*********************************************************************/


No comments:

Post a Comment