Saturday 17 September 2011

Doubly Linked List program with all the operations

Doubly Linked List program with all the operations:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
template<class T>
class node
{
 node<T> *llink;
 T data;
 node<T> *rlink;
 friend class dll<T>;
};
template <class T>
class dll
{
 private:
        node<T> *l,*r;
 public:
        dll();
       ~dll();
        int isempty();
        void display();
        int length();
        void insert(int i, T x);
        int del(int i);
        T find(int i);
node<T>* search(T x);
void erase();
void append(node<T>*k);
void create();
};
template<class T>
dll<T>::dll()
{
 l=r=NULL;
}
template<class T>
int dll<T>::isempty()
{
 return(l==NULL);
}
template<class T>
void dll<T>::display()
{

 node<T> *p;

 if(isempty())
 {
  cout<<"DLL is empty\n";
 }
 else
 {
   cout<<"DLL is as follows\n";
   for(p=l;p!=NULL;p=p->rlink)
     cout<<p->llink<<"\t"<<p->data<<"\t"<<p->rlink<<"\n";
 }
}
//length()
template<class T>
int dll<T>::length()
{
 int ctr=0;
 node<T> *p;
 p=l;
 while(p!=NULL)
 {
  ctr++;
  p=p->rlink;
 }
 return(ctr);
}
//find()

template<class T>
T dll<T>::find(int i)
{
 node<T> *p;
 if(i<1||i>length())
  return(-1);
 p=l;
 for(int j=1;j<=i-1;j++)
  p=p->rlink;
 return(p->data);
}
//search()
template<class T>
node<T>* dll<T>::search(T x)
{
 node<T> *p;
 p=l;
 while(p!=NULL)
 {
 if(p->data==x)
  return(p);
 }
 return(NULL);
}
//apend()
template <class T>
void dll<T>::append(node<T>* k)
{
 node<T> *last;
 k->rlink=NULL;
 if(isempty())
   l=r=k;
 else
 {
   last=l;
   while(last->rlink!=NULL)
   {
    last=last->rlink;
   }
   last->rlink=k;
   r=last;
  }
}
//create()
template<class T>
void dll<T>::create()
{
  T x;
  node<T> *k;
  l=r=NULL;
  cout<<"enter values terminated by -1 or !\n";
  cin>>x;
  while(x!=-1&&x!='!')
  {
   k=new node<T>;
   k->data=x;
   append(k);
   cin>>x;
   r=r->link;
  }
 }//create()
   //insert()

template<class T>
void dll<T>::insert(int i,T x)
{
 node<T> *temp,*p,*next;
 if(i<0||i>length())
  cout<<"Non existing node\n";
 else
 {
  p=new node<T>;
  p->data=x;
  if(isempty())
  {
   l=r=p;
   p->llink=p->rlink=NULL;
  }
  else if(i==0)
  {
   l->llink=p;
   p->rlink=l;
   p->llink=NULL;
   l=p;
  }
  else if(i==length())
  {
   p->rlink=NULL;
   p->llink=r;
   r->rlink=p;
   r=p;
  }
  else
  {
   temp=l;
   for(int j=1;j<=i-1;j++)
    temp=temp->rlink;
   next=temp->rlink;
   p->llink=temp;
   p->rlink=next;
   next->llink=p;
  }
 }
}
//erase()
template<class T>
void dll<T>::erase()
{
 node<T>*p;
 while(!isempty())
 {
  p=l;
  l=l->rlink;
  delete p;
 }
 r=NULL;
}
template<class T>
dll<T>::~dll()
{
 erase();
}
//dele()
template <class T>
T dll<T>::del(int i)
{
 node<T>*p,*prev,*next;
 T x;
 if(i<1||i>length())
  return(-1);
 else if(l==r)
 {
  p=l;
  l=r=NULL;
  x=p->data;
  delete p;
  return(x);
 }
 else if(i==1)
 {
  p=l;
  l=l->rlink;
  l->llink=NULL;
  x=p->data;
  delete p;
  return(x);
 }
 else if(i==length())
 {
  p=r;
  r=r->llink;
  r->rlink=NULL;
  x=p->data;
  delete p;
  return(x);
 }
 else
 {
  p=l;
  for(int j=1;j<=i-1;j++)
   p=p->rlink;
  prev=p->llink;
  next=p->rlink;
  prev->rlink=next;
  next->llink=prev;
  x=p->data;
  delete p;
  return(x);
 }
}

//menu()
void menu()
{
 cout<<"1. insert a node after ith node\n";
 cout<<"2. delete ith node\n";
 cout<<"3. display DLL\n";
 cout<<"4. length of DLL\n";
 cout<<"5. data of ith node in DLL\n";
 cout<<"6. search for a value in DLL\n";
 cout<<"7. erase DLL\n";
 cout<<"8. exit\n";
}

main()
{
 dll<int>a;
 node<int>*p;
 int ch,i,x;
 clrscr();
 a.create();
 menu();
 cout<<"enter ur choice\n";
 cin>>ch;
 while(ch<8)
 {
  switch(ch)
  {
   case 1:{
     cout<<"enter value of i( 0 to insert ar the beginning)\n";
     cin>>i;
     cout<<"enter value of new node\n";
     cin>>x;
     a.insert(i,x);
     break;
    }
    case 2:{
     cout<<"enter value of i\n";
     cin>>i;
     x=a.del(i);
     if(x==-1)
      cout<<"Non-existing node\n";
     else
       cout<<"data of deleted node is"<<x<<"\n";
     break;
    }
    case 3:{
          a.display();
          break;
           }
     case 4:{
           cout<<"no.of nodes ="<<a.length()<<"\n";
           break;
           }
     case 5:{
          cout<<"enter value of i\n";
          cin>>i;
          x=a.find(i);
          if(x==-1)
             cout<<"non existing node\n";
          else
               cout<<"data of desired node is"<<x<<"\n";
          break;
         }   
     
     case 6:{
          cout<<"enter value to be searched\n";
          cin>>x;
          p=a.search(x);
          if(p==NULL)
           cout<<"It is not found\n";
          else
          cout<<"it is found at address="<<p<<"\n";
          break;
           }
     case 7:{
            a.erase();
            a.display();
            break;
           }
        }//switch
   menu();
  cout<<"enter ur choice\n";
  cin>>ch;

  }//while


getch();
return(0);

}//main()

Doubly Linked List Operations
Types of Linked list
1.     Single Linked List


No comments:

Post a Comment