u

Wednesday 6 June 2012

C++ Program to create a doubly linked list

C++ Program to create a doubly linked list,to add an item(in front,in middle or in end),to delete an element(in front,in middle or in end) and to display to elements in the nodes.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class dlink
{
struct link
{
int data;
link *prev;
link *next;
}*head,*trav,*tptr;
int p,flag,a,p1,b;
public:

void creation(int);
void additem(int);
void deletion();
void display();
dlink()
{
head=NULL;
}
};

void dlink::creation(int i)
{
link *newlink=new(link);
if(head==NULL)
{
newlink->data=i;
newlink->prev=NULL;
newlink->next=NULL;
head=newlink;
}
else
{
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
newlink->prev=trav;
trav->next=newlink;

newlink->data=i;
newlink->next=NULL;
}
}

void dlink::additem(int i)
{
if(head==NULL)
cout<<"\n DOUBLEY LINKED LIST NOT CREATED";
else
{
cout<<" 1.Front, 2.Middle, 3.End \n";
cout<<" Your option : ";
cin>>p;
link *newlink=new(link);
switch(p)
{
case 1:
newlink->data=i;
newlink->next=head;
head->prev=newlink;
newlink->prev=NULL;
head=newlink;
break;
case 2:
cout<<"\nEnter the no. after which to be added : ";
cin>>a;
trav=head;
tptr=head;
while(trav->data!=a)
{
tptr=trav->next->next;
trav=trav->next;
}
newlink =new(link);
newlink->prev=trav;
newlink->next=tptr;
newlink->data=i;
tptr->prev=newlink;
trav->next=newlink;
break;

case 3:
trav=head;
while(trav->next!=NULL)
{
trav=trav->next;
}
trav->next=newlink;
newlink->prev=trav;
newlink->data=i;
newlink->next=NULL;
break;
}
}
}
void dlink::deletion()
{
if(head==NULL)
cout<<"\n DOUBLEY LINKED LIST NOT CREATED ";
else
{
cout<<" 1. Deletion in front, 2. Middle, 3.End \n";
cout<<"option : ";
cin>>p1;
switch(p1)
{
case 1:
if(head!=NULL)
{
trav=head;
trav=trav->next;
trav->prev=NULL;
head=trav;
}
else
cout<<"\n doubley linked list not created ";
break;
case 2:
if(head!=NULL)
{
cout<<"enter the element to be deleted : ";
cin>>b;
trav=head;
while(trav->data!=b)
{
tptr=trav;
trav=trav->next;
}
tptr->next=trav->next;
trav->next->prev=tptr;
}
else
cout<<"\n doubley linked list not created ";
break;
case 3:
trav=head;
while(trav->next!=NULL)
{
tptr=trav;
trav=trav->next;
}
tptr->next=NULL;
trav=tptr;
break;
}
}
}


void dlink::display()
{
if(head==NULL)
cout<<"\n NO ELEMENTS IN THE DOUBLEY LINKED LIST ";
else
{
cout<<" \n\n\t ELEMENTS IN THE DOUBLEY LINKED LIST ARE GIVEN BELOW : \n\n\n\n\t ";
trav=head;
while(trav->next!=NULL)
{
cout<<trav->data<<" <==> ";
trav=trav->next;
}
cout<<trav->data<<" <==> NULL";
}
}

void main()
{
dlink d;
int ch,x,y;
do
{
clrscr();
cout<<" \n\n\t\t PROGRAM FOR DOUBLEY LINKED LIST ";
cout<<"\n\n\t Menu \n\n";
cout<<"\n\t\t 1. Creation ";
cout<<"\n\t\t 2. Adding an element ";
cout<<"\n\t\t 3. Deleting an element ";
cout<<"\n\t\t 4. Display";
cout<<"\n\t\t 5. Exit \t";
cout<<"Opion : ";
cin>>ch;
switch(ch)
{
case 1:
do
{

cout<<"\nEnter a +ve no. enter -ve no. to stop the creation : ";
cin>>x;
if(x>0)
d.creation(x);
}
while(x>0);
break;

case 2:
cout<<"\n\nEnter the element : ";
cin>>y;
d.additem(y);
getch();
break;

case 3:
d.deletion();
getch();
break;
case 4:
d.display();
getch();
break;
case 5:
exit(0);
break;
default:
cout<<"\n Enter the option between 1 and 5 :";
break;
}
}
while(ch!=5);
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More