Professional Documents
Culture Documents
Doubly
Doubly
In computer science, a doubly linked list is a linked data structure that consists of a set of
sequentially linked record called nodes. Each node contains two fields, called links, that are
references to the previous and to the next node in the sequence of nodes. The beginning and
ending nodes' previous and next links, respectively, point to some kind of terminator, typically a
sentential node or null, to facilitate traversal of the list. If there is only one sentinel node, then
the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked list
formed from the same data items, but in opposite sequential orders.
A technique known as XOR-linking allows a doubly linked list to be implemented using a single link field
in each node. However, this technique requires the ability to do bit operations on addresses, and therefore
may not be available in some high-level languages. Double-linked lists require more space per node
(unless one uses XOR-linking), and their elementary operations are more expensive; but they are often
easier to manipulate because they allow sequential access to the list in both directions. In a doubly linked
list, one can insert or delete a node in a constant number of operations given only that node's address. To
do the same in a singly linked list, one must have the address of the pointer to that node, which is either
the handle for the whole list (in case of the first node) or the link field in the previous node. Some
algorithms require access in both directions. On the other hand, doubly linked lists do not allow tail-
sharing and cannot be used as persistent data structures.
ALGORITHM:
INPUT: The number of elements to create the list and the elements which are to be inserted.
The linked list after inserting an element at any position of the list.
The linked list after deleting an element from any position of the list.
PROCESS:
Step 3: Globally declare a variable ‘head’(a ‘dnode’ type pointer as defined in Step 2)
Step 4.1: Create a new node in ‘head’(a ‘dnode’ type pointer as defined in Step 2) by using ‘malloc’
function
Step 4.2: Take the number of elements in an integer type variable ‘n’ to create the list
Step 4.3: For i=0 to i<n repeat Step 4.4 to Step 4.7
Step 4.4: Take the element in an integer type variable ‘x’ to insert into the list
Step 4.5: Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using ‘malloc’ function.
Set data[p] x
Ser ptr p
[End of ‘If’]
Set right[ptr] p
Set ptr p
[End of ‘Else’]
Step 5.2: While(ptr≠Null) then repeat Step 5.3 and Step 5.4
Step 6.2: While(ptr≠NULL) then repeat Step 6.3 and Step 6.4
Step 6.5: Print the number of nodes i.e ‘n’ which is an integer type variable
Step 7.1: If(head=Null) then print “List does not exist” and return.
[End of ‘If’]
Step 7.3: While(ptr1≠Null) then repeat Step 7.4 and Step 7.5
Step 7.6: Take the position from the user in an integer type variable ‘pos’ at which position the
element will be inserted.
a) Set ptrleft[head]
b) Take the element in an integer type variable ‘x’ to insert into the list
c) Create a new node in ‘p’ (a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.
Set data[p] x
[End of ‘If’]
Step 7.8: Else if(pos=(n+1)) then
a) Set ptrright[head]
b) Take the element in an integer type variable ‘x’ to insert into the list
c) Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.
Set data[p] x
Set left[p] Null and Set right[p] Null
d) Set right[ptr] p
e) Set left[p] ptr
f) Set right[head] p
a) Set ptrleft[head]
b) While(pos>c) then repeat
Set c c+1
Set ptrright[ptr]
[End of ‘While’]
c) Take the element in an integer type variable ‘x’ to insert into the list
d) Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.
Set data[p] x
e) Set right[ptr1] p
g) Set left[ptr] p
[End of ‘Else’]
Step 8.1: If(head=Null) then print “List does not exist” and return.
[End of ‘If’]
Step 8.6: Take the position from the user in an integer type variable ‘pos’ at which position the
element will be inserted.
a) Set ptrleft[head]
b) Set left[head] right[ptr]
c) Set left[right[ptr]] Null
d) Set right[ptr] Null
e) Free ptr.
[End of ‘If’]
a) Set ptrright[head]
b) Set right[head] left[ptr]
c) Set right[left[ptr]] Null
d) Set left[ptr] Null
e) Free ptr
a) Set ptrleft[head]
b) While(pos>c) then repeat
Set c c+1
Set ptrright[ptr]
[End of ‘While’]
[End of ‘Else’]
Step 9.5: Call the functions ‘1. createlist()’ , ‘ 2. displaylist()’ , ‘3. nodecount()’ ,
‘4.insertanyposition()’, ‘5. delfromanyposition()’ , ‘6. Exit’ depending on the input ‘ch’.
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
int data;
}dnode;
dnode *head;
void createlist()
dnode *ptr,*p;
int i,n,x;
head =(dnode*)malloc(sizeof(dnode));
head->left=head->right=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
p=(dnode*)malloc(sizeof(dnode));
p->left=p->right=NULL;
p->data=x;
if(i==0)
head->right=head->left=p;
ptr=p;
else
ptr->right=p;
p->left =ptr;
ptr=p;
head->right=ptr;
void displaylist()
dnode *ptr;
ptr=head->left;
printf("\n");
while(ptr!=NULL)
printf("%d->",ptr->data);
ptr=ptr->right;
printf("\n");
void nodecount()
int n=0;
dnode *ptr;
ptr=head->left;
while(ptr!=NULL)
n++;
ptr=ptr->right;
void insertanyposition()
int x,pos,c=1,n=0;
dnode *p,*ptr,*ptr1;
if(head==NULL)
return;
}
ptr1=head->left;
while(ptr1!=NULL)
n++;
ptr1=ptr1->right;
scanf("%d",&pos);
if(pos==1)
ptr=head->left;
scanf("%d",&x);
p=(dnode*)malloc(sizeof(dnode));
p->data=x;
p->left=p->right=NULL;
p->right=ptr;
ptr->left=p;
head->left=p;
else if(pos==(n+1))
ptr=head->right;
scanf("%d",&x);
p=(dnode*)malloc(sizeof(dnode));
p->data=x;
p->left=p->right=NULL;
ptr->right=p;
p->left=ptr;
head->right=p;
else
ptr=head->left;
while(pos>c)
c++;
ptr1=ptr;
ptr=ptr->right;
scanf("%d",&x);
p=(dnode*)malloc(sizeof(dnode));
p->data=x;
p->left=p->right=NULL;
ptr1->right=p;
p->left=ptr1;
ptr->left=p;
p->right=ptr;
displaylist();
//* Delete an element from any position of the linked list *//
void delfromanyposition()
int pos,c=1,n=0;
dnode *ptr,*ptr1;
if(head==NULL)
return;
ptr1=head->left;
while(ptr1!=NULL)
n=n+1;
ptr1=ptr1->right;
scanf("%d",&pos);
if(pos==1)
ptr=head->left;
head->left=ptr->right;
(ptr->right)->left=NULL;
ptr->right=NULL;
free(ptr);
else if(pos==n)
ptr=head->right;
head->right=ptr->left;
(ptr->left)->right=NULL;
ptr->left=NULL;
free(ptr);
else
{
ptr=head->left;
while(pos>c)
c++;
ptr1=ptr;
ptr=ptr->right;
ptr1->right=ptr->right;
(ptr->right)->left=ptr1;
ptr->left=ptr->right=NULL;
free(ptr);
printf("\nAfter deleting from the %dth position the list is: ",pos);
displaylist();
void main()
int ch;
do
scanf("%d",&ch);
switch(ch)
createlist();
break;
case 2:printf("\n Display list:\n");
displaylist();
break;
nodecount();
break;
insertanyposition();
break;
delfromanyposition();
break;
case 6:exit(0);
}while(1);
OUTPUT:
DISCUSSION:
The first and last nodes of a doubly linked list are immediately accessible (i.e., accessible without
traversal, and usually called head and tail) and therefore allow traversal of the list from the
beginning or end of the list, respectively: e.g., traversing the list from beginning to end, or from
end to beginning, in a search of the list for a node with specific data value. Any node of a doubly
linked list, once obtained, can be used to begin a new traversal of the list, in either direction
(towards beginning or end), from the given node.
The two node links allow traversal of the list in either direction. While adding or removing a
node in a doubly linked list requires changing more links than the same operations on a singly
linked list, the operations are simpler and potentially more efficient (for nodes other than first
nodes) because there is no need to keep track of the previous node during traversal or no need
to traverse the list to find the previous node, so that its link can be modified.