Saturday, February 7, 2015

Doubly linked list all operations

#include<iostream>
#include<cstdlib>
using namespace std;
struct dnode
{
int data;
dnode *right;
dnode *left;
};
void display(dnode *s)
{
dnode *p;
p=s;
cout<<"\tThus the no's are as:\n";
while(p!=NULL)
{cout<<"\t"<<p->data<<endl;
p=p->right;
}
}
dnode *add_end(dnode *s,int d)
{
dnode *p,*l;
l=s;
while(l->right!=NULL)
l=l->right;
p=new(dnode);
p->data=d;
p->right=NULL;
p->left=NULL;
l->right=p;
p->left=l;
return s;
}
dnode *add_begin(dnode *s,int d)
{
dnode *p,*l;
p=s;
l=new(dnode);
l->data=d;
l->right=p;
l->left=NULL;
p->left=l;
s=l;
return s;
}
dnode *add_after(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
while(p->data!=d)
p=p->right;
if(p->right==NULL)
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=p;
p->right=l;
return s;
}
else
{
l=new(dnode);
l->data=k;
l->right=NULL;
l->left=NULL;
l->right=p->right;
l->left=p;
p->right=l;
l->right->left=l;
return s;
}
}
dnode *add_before(dnode *s,int d,int k)
{
dnode *p,*l;
p=s;
if(p->data==d)
{
l=new(dnode);
l->data=k;
    l->right=p;
   l->left=NULL;
p->left=l;
s=l;
return s;
}
else
{
while(p->data!=d)
p=p->right;
l=new(dnode);
l->data=k;
l->left=p->left;
l->right=p;
p->left=l;
l->left->right=l;
return s;
}
}
dnode *del_end(dnode *s)
{
dnode *p;
p=s;
while(p->right->right!=NULL)
p=p->right;
p->right=NULL;
return s;
}
dnode *del_begin(dnode *s)
{
dnode *p;
p=s;
p=p->right;
p->left=NULL;
s=p;
return s;
}
dnode *del_mid(dnode *s,int d)
{
dnode *p;
p=s;
while(p->right->data!=d)
p=p->right;
p->right=p->right->right;
p->right->left=p;
return s;
}
int main()
{
dnode *s,*t,*l;
int d,k,n;
l=new(dnode);
s=l;
    cout<<"Start entering the no's.....Enter -1 for aborting...\n";
    cin>>d;
    l->data=d;
    l->left=NULL;
    l->right=NULL;
    cin>>d;
    while(d!=-1)
     {t=new(dnode);
      t->data=d;
      t->right=NULL;
      t->left=NULL;
      l->right=t;
      t->left=l;
      l=t;
      cin>>d;
     }
    display(s);
    while(1)
{
  cout<<"Select any of the options provided below....\n1:Adding at the end\n2:Adding at the beginning\n3:Adding after a specified no\n4:Adding before a specified no\n5:Deleting a no at the end\n6:Deleting a no at the beginning\n7:Deleting a no in between the no's\n8:For quitting\n";
  cin>>n;
       switch(n)
       {case 1 :cout<<"Enter the no:";
                cin>>d;
                s=add_end(s,d);
                display(s);
                break;
        case 2 :cout<<"Enter the no:";
                cin>>d;
                s=add_begin(s,d);
                display(s);
                break;
        case 3 :cout<<"Enter the no after which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_after(s,d,k);
       display(s);
break;
case 4 :cout<<"Enter the no before which you want to enter:";
       cin>>d;
       cout<<"Enter the no:";
       cin>>k;
       s=add_before(s,d,k);
       display(s);
       break;
case 5 :s=del_end(s);
       display(s);
       break;
case 6 :s=del_begin(s);
       display(s);
       break;
case 7 :cout<<"Enter the no which you want to delete:";
       cin>>d;
       s=del_mid(s,d);
       display(s);
       break;
case 8 :cout<<"Thanks...";
       exit(1);
        default:cout<<"Wrong choice....Enter again\n";
                break;
       }
    }
}

No comments:

Post a Comment

Contributors

Translate