DOUBLY Linked List

/*
DOUBLY Linked list.
*/
#include<iostream.h>
#include<conio.h>

struct dnode
{
int info;
dnode *prev,*next;
};
class Doublylinkedlist
{
private:
dnode *start;
public:
Doublylinkedlist();
void createlist();
void traversal();
void insertbegin();
void insertlast();
void insertafter();
void insertbefore();
void split(int);
};
Doublylinkedlist::Doublylinkedlist()
{
start=NULL;
}
void Doublylinkedlist::createlist()
{
dnode *nnode,*last;
char c;
last=NULL;
do
{
nnode=new dnode;
cout<<“\nEnter Data: “;
cin>>nnode->info;
nnode->prev=NULL;
nnode->next=NULL;
if(start==NULL)
{
start=nnode;
}
else
{
last->next=nnode;
nnode->prev=last;
}
last=nnode;
cout<<“\nDo you want to continue:(Y/N) “;
cin>>c;

}while(c==’y’||c==’Y’);
}

void Doublylinkedlist::traversal()
{
dnode *temp;
temp=start;
cout<<“\nContents of List are: “;
while(temp!=NULL)
{
cout<<temp->info<<“,”;
temp=temp->next;
}
}
void Doublylinkedlist::insertlast()
{
dnode *temp, *nnode;

nnode=new dnode;
cout<<“\nEnter Data: “;
cin>>nnode->info;
nnode->next=NULL;
if(start==NULL)
{
start=nnode;
nnode->prev=NULL;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nnode;
nnode->prev=temp;
}
}
void Doublylinkedlist::insertbegin()
{
dnode *nnode;
nnode=new dnode;
cout<<“\nEnter Data: “;
cin>>nnode->info;
nnode->prev=NULL;
if(start==NULL)
{
nnode->next=start;
}
else
{
nnode->next=start;
start->prev=nnode;
}
start=nnode;

}
void Doublylinkedlist::insertafter()
{
int key;
dnode *temp,*nnode;
cout<<“\nEnter the Information after which you want to insert: “;
cin>>key;
temp=start;
while(temp!=NULL)
{
if(key==temp->info)
{
nnode=new dnode;
cout<<“\nEnter Data: “;
cin>>nnode->info;
nnode->next=temp->next;
nnode->prev=temp;
temp->next=nnode;
break;
}
else
{
temp=temp->next;
}
}
if(temp==NULL)
{
cout<<“\nKey not Found.”;
}
}
void Doublylinkedlist::insertbefore()
{
int key;

cout<<“\nEnter the information before which you to insert node: “;
cin>>key;

dnode *temp,*nnode;
temp=start;
while(temp!=NULL)
{
if(key==temp->info)
{
nnode=new dnode;
cout<<“\nEnter Data: “;
cin>>nnode->info;
if(temp->prev==NULL)
{
temp->prev=nnode;
nnode->prev=NULL;
start=nnode;
}
else
{
nnode->prev=temp->prev;
temp->prev->next=nnode;
temp->prev=nnode;
}
nnode->next=temp;
break;
}
else
{
temp=temp->next;
}
}
if(temp==NULL)
{
cout<<“\nKey not Found.”;
}

}
void Doublylinkedlist::split(int key)
{
dnode *temp,*start2,*temp2;

if(start==NULL)
{
cout<<“\nSplit not possible.”;
}
else
{
while(temp!=NULL)
{
if(temp->info==key)
{
break;
}
else
{
temp=temp->next;
}
}
if(temp==NULL)
{
cout<<“\nKey Not Found.”;
}
if(start==temp)
{
cout<<“\nSplit not Possible.”;
}
else
{
start2=temp;
temp->prev->next=NULL;
temp->prev=NULL;
}
temp=start;
temp2=start2;
cout<<“\nList 1: \n”;
while(temp!=NULL)
{
cout<<temp->info<<“,”;
temp=temp->next;
}
cout<<“\nList 2: \n”;
while(temp2!=NULL)
{
cout<<temp->info<<“,”;
temp=temp->next;
}
}
}

int main()
{
Doublylinkedlist dl;
int n,c;
char s;
clrscr();

do
{
cout<<“\n1.Createlist: “;
cout<<“\n2.Traverse List: “;
cout<<“\n3.InsertBegin: “;
cout<<“\n4.InsertLast: “;
cout<<“\n5.InsertBefore:”;
cout<<“\n6.InsertAfter: “;
cout<<“\n7.Split.”;
cout<<“\nEnter your choice: “;
cin>>c;
switch(c)
{
case 1: dl.createlist();
break;
case 2: dl.traversal();
break;
case 3: dl.insertbegin();
break;
case 4: dl.insertlast();
break;
case 5: dl.insertbefore();
break;
case 6: dl.insertafter();
break;
case 7: cout<<“\nEnter value to Split from: “;
cin>>n;
dl.split(n);
break;
default:cout<<“\nInvalid Option.”;
};

cout<<“\n\nDo You want to continue(Y/N): “;
cin>>s;
}while(s==’y’||s==’Y’);

getch();

return(0);
}
/*
OUTPUT:
——-
1.Createlist:
2.Traverse List:
3.InsertBegin:
4.InsertLast:
5.InsertBefore:
6.InsertAfter:
7.Split.
Enter your choice: 1

Enter Data: 2

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

Enter Data: 3

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

Enter Data: 4

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

Enter Data: 5

Do you want to continue:(Y/N) n
Do You want to continue(Y/N): y

1.Createlist:
2.Traverse List:
3.InsertBegin:
4.InsertLast:
5.InsertBefore:
6.InsertAfter:
7.Split.
Enter your choice: 2

Contents of List are: 2,3,4,5,

Do You want to continue(Y/N): n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.