Professional Documents
Culture Documents
11
Theory: A doubly-linked list is a linked data structure that consists of a set of data
records, each having two special link fields that contain references to the previous and to
the next record in the sequence, that means each node has two links from both direction
and so it is called doubly linked.
Doubly linked lists is a special case of linked list in which the single node of it hold the
address of the next node to current node and the address of the previous node of the
current node. Here each record of the list is represented by node which in this case
contains three fields, first is the previous field which is a pointer that holds the address of
previous node ie previous and the second field is the information field ie. info which
contains the actual element on the list which may be simple integer, a character, a string
or even a large record, and third is next field ie. next which is a pointer contains the
address of next node the list. The structure of node of a linked list is shown in Fig.1.
Entire linked list can be accessed by an external pointer to the list pointing to the first
node of the list. So we can access the first node through the external pointer and the
second node through the next pointer of the first node and the previous pointer of the
third node. The previous field of the first node is set to be NULL that it holds no address
and the next field of the last node also contains a NULL value.
The whole structure of doubly linked list is shown in Fig.2 below:
Algorithm:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct list
{
int info;
struct list *next;
struct list *prev;
}*start=NULL,*last=NULL,*node,*temp,*newnode;
void main()
{
clrscr();
int ch;
printf("\n1. create_list");
printf("\n2. display_list");
printf("\n3. insert_first");
printf("\n4. insert_last");
printf("\n5. insert_desir");
printf("\n6. insert_mid");
printf("\n7. delete_first");
printf("\n8. delete_last");
printf("\n9. delete_desir");
printf("\n10. delete_mid");
printf("\n11. count_node");
printf("\n12. search_node");
printf("\n13. quit");
printf("\n\nEnter choise");
scanf("%d",&ch);
switch(ch)
{
case 1:
node=(struct list *)malloc(sizeof(struct list));
node->prev=NULL;
node->next=NULL;
start=last=node;
create_list(start);
break;
case 2:
display_list(start);
break;
case 3:
insert_first();
break;
case 4:
insert_last();
break;
case 5:
insert_desir();
break;
case 7:
delete_first();
break;
case 8:
delete_last();
break;
case 9:
delete_desir();
break;
case 13:
exit(0);
default:
printf("\nInvalid choise");
break;
}
getch();
main();
}
void insert_first()
{
if(start==NULL)
printf("\nList empty");
else
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->prev=NULL;
newnode->next=start;
start->prev=newnode;
start=newnode;
}
}
void insert_last()
{
if (last==NULL)
printf("\nList empty");
else
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->next=NULL;
newnode->prev=last;
last->next=newnode;
last=newnode;
}
}
void insert_desir()
{
if(start==NULL)
printf("\nList empty");
else
{
int nodeno;
printf("\nEnter nodeno after which you want to insert");
scanf("%d",&nodeno);
node=start;
while(node)
{
if(node->info==nodeno)
{
newnode=(struct list *)malloc(sizeof(struct list));
printf("\nEnter info");
scanf("%d",&newnode->info);
newnode->next=node->next;
node->next->prev=newnode;
newnode->prev=node;
node->next=newnode;
if(last==node)
last=newnode;
}
node=node->next;
}
}
}
void delete_first()
{
if(start==NULL)
printf("\nList empty");
else
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
}
void delete_last()
{
if(start==NULL)
printf("\nList empty");
else
{
temp=last;
last=last->prev;
last->next=NULL;
free(temp);
}
}
void delete_desir()
{
if(start==NULL)
printf("\nList empty");
else
{
int nodeno;
printf("\nEnter nodeno which you want to delete");
scanf("%d",&nodeno);
if(start->info==nodeno)
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
else if(last->info==nodeno)
{
temp=last;
last=last->prev;
last->next=NULL;
free(temp);
}
else
{
node=start;
while(node)
{
if(node->info==nodeno)
{
temp=node;
node->prev->next=node->next;
node->next->prev=node->prev;
node=node->next;
free(temp);
}
node=node->next;
}
}
}
}
Program Output:
I.Creating List