Double linked list in C++ complete concept

A type of list where  each node is connected with its previous one and next one.Address of previous node and address of next node is also saved along data inside node.So in double linked list we can access elements from back side and elements from front side by staying on the same node.Copy the code from below and paste it into your editor.Once you have compiled and executed code successfully, you will see the following menu.
  • Enter 2 to Display
  • Enter 3 to delete node from start
  • Enter 4 to delete node from last
  • Enter 5 to insert node at start
  • Enter 6 to insert node at last
  • Enter 7 to insert node after specific node
 By using this menu you can perform many options like insertion deletion and modification of double linked list.But be careful deletion process is not same as single linked list.You need to add the address of previous node in  the left link of front node.In the same way insertion process can be created successfully.
 

#include<iostream>
#include<conio.h>
using namespace std;

struct node
{
int data;
node *right_link;
node *left_link; 
}*head=NULL;

void create()
{
 
 int info;
node *new1=head;
node *end=head;

cout<<"Enter -5 to terminate link list:"<<endl;

while(info!=-5)
{
 
cout<<"Enter you data : ";
cin>>info;
new1=new node;
new1->data=info;
if(head==NULL)
{
head=new1;
new1->left_link=NULL;
new1->right_link=NULL;
}

else
{
end->right_link=new1;
new1->left_link=end;
new1->right_link=NULL;

}
end=new1;
}
 
}


void display()
{
node *p=head;
if(head==NULL or head->right_link==NULL)
cout<<"link list is empty"<<endl;
else
{

cout<<" \n\nYour link list is : ";
while(p->right_link!=NULL)
{
cout<<" "<<p->data;
p=p->right_link;
 
}}}


void dfhead()
{
 
node *p=head;
if(head==NULL or head->right_link==NULL)
cout<<"link list is empty"<<endl;
else
{

head=p->right_link;
head->left_link=NULL;
cout<<"Your desired node gets deleted"<<endl;
delete(p);
}}

void dflast()
{
node *p=head;
if(head==NULL or head->right_link==NULL)
cout<<"link list is empty"<<endl;
else
{
while(p->right_link!=NULL)
{
p=p->right_link;
 
} 
p->left_link->right_link=NULL;
cout<<"Your desired node gets deleted"<<endl;
delete(p);
}}


void iafirst()
{
 int info;
if(head==NULL)
{

cout<<"Node created Enter node data: ";
cin>>info;
node *new1=new node; 
new1->data=info;
new1->left_link=NULL;
new1->right_link=NULL;
head=new1;
cout<<"Process successfull"; 
}

else
{

node *new1=new node;
head->left_link=new1;
new1->right_link=head;
new1->left_link=NULL;
head=new1;
cout<<"Node created Enter node data: ";
cin>>info; 
head->data=info;
cout<<"Process successfull";
}}


void ialast()
{
 int n;
node *p=head;

while(p->right_link!=NULL)
{
p=p->right_link;
 
} 
node *new2=new node;
new2->right_link=NULL;
new2->left_link=p;
p->right_link=new2;
cout<<"Node created Enter node data: ";
cin>>n; 
new2->data=n;
cout<<"Process successfull";
 
}


void iainter()
{
 int c,n,count=1;
node *p=head;
cout<<"Enter node num after which you want another node:";
cin>>c;
while(count!=c)
{
p=p->right_link;
count++;
 
 }
node *new1=new node;
new1->right_link=p->right_link;
new1->left_link=p;
p->right_link->left_link=new1;
p->right_link=new1;
cout<<"Node created Enter node data: ";
cin>>n; 
new1->data=n;
cout<<"Process successfull"; 
  
}

main()
{

cout<<"\t***Welcome to Double linked list***"<<endl;
int choice;
while(choice!=99)
{

cout<<"\n\nEnter 1 to create "<<endl;
cout<<"Enter 2 to Display"<<endl;
cout<<"Enter 3 to delete node from start"<<endl;
cout<<"Enter 4 to delete node from last"<<endl; 
cout<<"Enter 5 to insert node at start"<<endl; 
cout<<"Enter 6 to insert node at last"<<endl;
cout<<"Enter 7 to insert node after intermidate"<<endl; 
cin>>choice;
switch(choice)
{
case 1:
create();
break;

case 2:
display();
break;

case 3:
dfhead();
break; 

case 4:
dflast();
break;

case 5:
iafirst();
break;

case 6:
ialast();
break;

case 7:
iainter();
break;

default:
cout<<"wrong input"<<endl; 
}

}
getch(); 
}

Output of Program:

Reactions

Post a Comment

0 Comments