Doubly linked list
Doubly linked list.
In doubly linked list each node is divided into three parts.1.LINK:- It holds address of previous node of linked list.
2.INFO:-It holds value of node.
3.RLINK:-It holds address of next node
In doubly linked list LINK part of first node and RLINK of last node always contain NULL.
Advantage:
It is use to avoid the drawback of singly linked list.we know that in singly linked list we can traversed node left to right/foreword direction but doubly linked list supports bidirectional traversing/both backward and foreword direction
Disadvantage:
It uses more memory space rather than singly linked list because it consist left and right pointer.
(If you not read previous topic singly linked list click on link)↧
singly linked list program choice based
Doubly linked list program choice base#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int info;
struct node *llink, * rlink;
};
void create();
void display();
void sinsert();
void pinsert();
void einsert();
void sdel();
void pdel();
void edel();
struct node *first=NULL,*last=NULL,*new1;
int main()
{
int choice;
do
{
system("cls");
printf("Press 1 for create node.\npress 2 for display list.\npress 3 for enter node at first position\npress 4 for insert at perticular position\npress 5 to insert at last position\npress 6 to delete first node\npress 7 to delete perticular position\npress 8 to delete last node\npress 9 for exit .........-->\t");
scanf("%d",&choice);
switch( choice)
{
case 1:
create(); break;
case 2:
display();break;
case 3:
sinsert(); break;
case 4:
pinsert(); break;
case 5:
einsert(); break;
case 6:
sdel(); break;
case 7:
pdel();break;
case 8:
edel(); break;
case 9:
exit(0);
default:
printf("invalid ! choice");
}
}
while(choice>=1 &&choice<=8);
return 0;
}
// create a new node
void create(){
new1=(struct node*)malloc (sizeof(struct node));
printf("enter the value of node=");
scanf("%d",&new1->info);
new1->rlink=NULL;
new1->llink=NULL;
if(first== NULL)
{
first=new1;
last=new1;
}
else
{
last->rlink=new1;
new1->llink=last;
last=new1;
}
}
//display in doubly linked list
void display()
{
struct node *temp;
if(first== NULL)
printf("there is no any node !");
else{
printf("all nodes are=\n");
for(temp=first;temp!=NULL;temp=temp->rlink)
{
printf("\t%d\n",temp->info);
}
}
system("pause");
}
//insert at first position in doubly linked list
void sinsert()
{
new1=(struct node*)malloc (sizeof(struct node));
printf("enter value of new node=");
scanf("%d",&new1->info);
new1->llink=NULL;
new1->rlink=first;
first->llink=new1;
first=new1;
}
//insert at particular position in doubly linked list
void pinsert()
{
new1=(struct node*)malloc(sizeof(struct node));
struct node *temp;
int position,loop=1,count1=0;
printf("enter position where you want to insert");
scanf("%d",&position);
for(temp=first;temp!=NULL;temp=temp->rlink)
{
count1++;
}
if(position>=count1||position<0)
printf("position not exist!");
else{
printf("enter value of node");
scanf("%d",&new1->info);
temp=first;
if(position==1)
{
new1->llink=NULL;
new1->rlink=first;
first->llink=new1;
first=new1;
}
else{
while(loop<position-1)
{
temp=temp->rlink;
loop++;
}
new1->llink=temp;
new1->rlink=temp->rlink;
temp->rlink=new1;
}
}
system("pause");
}
//insert at last position in doubly linked list
void einsert()
{
new1=(struct node*)malloc(sizeof(struct node));
printf("enter the the value");
scanf("%d",&new1->info);
new1->rlink=NULL;
last->rlink=new1;
last=new1;
}
//delete at first position in doubly linked list
void sdel()
{
printf("record deleted from first is=%d\n",first->info);
first=first->rlink;
system("pause");
}
//delete at particular position in doubly linked list
void pdel()
{
struct node *temp,*pre;
int position,loop=1,count1=0;
printf("enter position which you want to delete");
scanf("%d",&position);
for(temp=first;temp!=NULL;temp=temp->rlink)
{
count1++;
}
if(position>=count1||position<0)
printf("position not exist!");
else{
temp=first;
if(position==1)
{
first=first->rlink;
}
else{
while(loop<position)
{
pre=temp;
temp=temp->rlink;
loop++;
}
pre->rlink=temp->rlink;
temp=temp->rlink;
temp->llink=pre;
}
}
system("pause");
}
//delete at last position in doubly linked list
void edel()
{
if(first==NULL)
printf("no any node avialable");
else{
printf("deleted record=%d\n",last->info);
last=last->llink;
last->rlink=NULL;
}
system("pause");
}
Comments
Post a Comment