Friday, June 14, 2013

DOUBLY LINKED LIST

DOUBLY_LINKED LIST (C++ CODE):

//To perform various operations on DLL

#include<iostream.h>
#include<conio.h>
class dll
{
 private:
 typedef struct node
 {
  int data;
  node *next;
  node *prev;
 };

 node *head;
 public:
 void create();
 void disp();
 int count();
 void search();
 void insert_front();
 void insert_end();
 void insert_mid();
 void sort();
 void con(dll b);
 void delfront();
 void delend();
 void delmid(int pos);

};

void dll::create()
{
  int n;
  node *p,*q;
  head=NULL;
  cout<<"\nenter number of nodes:";
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cout<<"\nenter data:";
    p=new node;
    cin>>p->data;
    p->next=NULL;
    p->prev=NULL;
    if(head==NULL)
      head=p;
    else
    {
     q=head;
     while(q->next!=NULL)
  q=q->next;
     q->next=p;
     p->prev=q;
    }
  }
}

void dll::disp()
{
  node *p;
  p=head;
  cout<<"\nlist is:";
  while(p!=NULL)
  {
    cout<<"\t"<<p->data;
    p=p->next;
  }
}

int dll:: count()
{
  node *p;
  int cnt=0;
  p=head;
  while(p!=NULL)
  {
    cnt++;
    p=p->next;
  }
  return(cnt);
}

void dll::search()
{
 node *p;
 int num,cnt=0;
 p=head;
 cout<<"enter the number to be searched:";
 cin >>num;
 while(p!=NULL)
 {
  cnt++;
  if(p->data==num)
  cout<<"number is at pos:"<<cnt;
  p=p->next;
 }
}

void dll::insert_front()
{
  node *p;
  p=new node;
  cout<<"\n enter data:";
  cin>>p->data;
  p->next=head;
  p->prev=NULL;
  head=p;
}

void dll::insert_end()
{
 node *p,*q;
 p=new node;
 cout<<"\n enter data:";
 cin>>p->data;
 p->next=NULL;
 q=head;
 while(p->next!=NULL)
 q=q->next;
 q->next=p;
 p->prev=q;
}

void dll::insert_mid()
{
  int loc;
  node *p,*q;
  cout<<"\n enter location:";
  cin>>loc;
  q=head;
  for(int i=1;i<=loc;i++)
  {
   q=q->next;
  }
  if(q==NULL)
  {
   cout<<"less than"<<loc<<"nodes";
   return;
  }
  p=new node;
  cout<<"\n enter data:";
  cin>>p->data;
  q->next=p;
  p->next=q->next;
  p->prev=q;
}

void dll::sort()
{
  node *p,*q;
  int num;
  for(p=head;p!=NULL;p=p->next)
  {
   for( q=p->next;q!=NULL;q=q->next)
   {
    if(p->data>q->data)
    {
     num=p->data;
     p->data=q->data;
     q->data=num;
    }
   }
  }
}

void dll::con(dll b)
{
  node *p;
  p=head;
  while(p->next!=NULL)
  p=p->next;
  p->next=b.head;
  disp();

}

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

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

void dll::delmid(int pos)
{
 node *p,*q;
 p=head;
 for(int i=1;i<pos;i++)


  p=p->next;

 if(p==NULL)
 {
  cout<<"less than"<<pos<<"nodes";
  return;
 }
 else
 {
  p->next->prev=p->prev;
  p->prev->next=p->next;
  delete(p);
 }
}


void main()
{
 int op,cnt,op1,op2,pos;
  char ch;
  clrscr();
  dll l1,a,b;
 do
 {
  cout<<"\n\n***menu***";
  cout<<"\n1.create\n2.display\n3.count\n4.search\n5.insert\n6.sort\n 7.concatenate\n 8.delete";
  cout<<"\nenter ur choice:";
  cin>>op;
  switch(op)
  {
   case 1:
   l1.create();
   break;
   case 2:
   l1.disp();
   break;
   case 3:
   cnt=l1.count();
   cout<<"\nnumber of nodes: "<<cnt;
   break;
   case 4:
   l1.search();
   break;
   case 5:
   cout<<"\n 1.insert at front\n2.insert at end\n3.insert at mid";
   cout<<"\n enter your choice:";
   cin>>op1;
   switch(op1)
   {
    case 1:
    l1.insert_front();
    break;
    case 2:
    l1.insert_end();
    break;
    case 3:
    l1.insert_mid();
    break;
   }
    break;
    case 6:
    l1.sort();
    cout<<"\n sorted list is:";
    l1.disp();
    break;
    case 7:
    a.create();
    b.create();
    a.con(b);
    break;
    case 8:
    cout<<"\n1.front\n2.end\n3.mid";
    cout<<"\n enter your choice:";
    cin>>op2;
    switch(op2)
    {
     case 1:
     l1.delfront();
     break;
     case 2:
     l1.delend();
     break;
     case 3:
     cout<<"\n enter the pos to be deleted:";
     cin>>pos;
     l1.delmid(pos);
     break;
    }
    break;
   }
  cout<<"\ndo u want to continue?:";
  cin>>ch;
 }while(ch=='y'||ch=='Y');

}

No comments:

Post a Comment

Thanks for your valuable comment