You are on page 1of 17

Doubly Linear LINKED LIST

• A doubly linked list is one in which all nodes are linked


together by multiple number of links which help in accessing
both the successor node and predecessor node from the
given node position
• It provides bi-directional traversing
• Each node in a doubly linked list has two link fields. These are
used to point to the successor node and predecessor node

NULL prev info next NULL


Insertion Operation on Doubly Linear
Linked list
• Inserting a node at the beginning of the list
• Inserting a node at the end of the list
• Inserting a node before or after specified
position
Inserting a node at the beginning of
the Doubly Linear Linked List
1. Declare a head pointer “pfirst” and make it as NULL.
pfirst=NULL
2. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data
3. If list is empty i.e if(pfirst==NULL) make the new node as first node and
last node
pfirst=pnew
plast=pnew
and make the link fields “next” and “prev”of new node point to NULL
pfirst->next=NULL
pfirst->prev=NULL
4. Else
make the “prev” field of newnode point to NULL
pnew->prev=NULL
Make the new node points to the first node “pfirst”.
pnew->next=pfirst
and make the first node points to newnode
pfirst->prev=pnew
5. make the new node as the first node.
pfirst=pnew
Inserting a node at the beginning of
Linear Doubly Linked List
pfirst

,plast=pnew
pnew pfirst ,plast

,plast

pnew pfirst

plast
Inserting a node at the end of the list
1. Declare a head pointer “pfirst” and make it as NULL.
pfirst=NULL
2. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data
3. If list is empty i.e if(pfirst==NULL) make the new node as first node and last node
pfirst=pnew
plast=pnew
and make the link fields “next” and “prev”of new node point to NULL
pfirst->next=NULL
pfirst->prev=NULL
4. Else
a. Make the next field of last node point to new node and make the prev field of new node point
to last node. Also make the next field of new node point to NULL
plast->next=pnew
pnew->prev=plast
pnew->next=NULL
b. Make the new node as last node
plast=pnew
Inserting a node at the end of the
doubly linear linked list
pfirst ,pthis
plast

pfirst pthis plast

plast
pfirst
Inserting a node before the specified
position in the list
1 . Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data

2. Input the specified location of a node “loc”


3. Initialize the new pointer “pthis” with “pfirst” and move the pointer “pthis” until it points to the
specified node

pthis=pfirst
for(i=1;i<=loc-1;i++)
pthis=pthis->next

4. Store the address of node just before the specified node in temporary pointer “ptemp”
ptemp=pthis->prev

make the next field of the node pointed by “ptemp” point to newnode and make the prev field of new
node
point to node pointed by ptemp
ptemp->next=pnew
pnew->prev=ptemp

Also make the next field of new node point to the node ,pointed by pthis. And make the prev field of
node pointed by pthis point to new node
pnew->next=pthis
pthis->prev=pnew
Inserting a node before the specified position in the
Doubly Linear Linked list
pfirst
Node2 Node3 (specified node)
Node1 plast
Loc=3

pthis
ptemp plast
pfirst pthis
ptemp

plast

pfirst pthis
Inserting a node after the specified position in the
Doubly Linear Linked list
1. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data

2. Input the specified location of a node “loc”


3. Initialize the new pointer “pthis” with “pfirst”and move the pointer “pthis” until it points to the the
specified position

pthis=pfirst
for(i=1;i<=loc-1;i++)
pthis=pthis->next

4. Store the address of node just after the specified node in temporary pointer “ptemp”
ptemp=pthis->next

make the prev field of the node pointed by “ptemp” point to newnode and make the next field of
newnode point to the node pointed by ptemp
point to node pointed by ptemp
ptemp->prev=pnew
pnew->next=ptemp

Also make the prev field of new node point to the node ,pointed by pthis. And make the next field of
node pointed by pthis point to newnode
pnew->prev=pthis
pthis->next=pnew
Inserting a node after the specified
position in the Doubly Linear Linked list
pfirst 2
Node2 (specified node) Node3
Node1 plast

Loc=2

ptemp
plast
pfirst pthis
ptemp

plast

pfirst pthis
Deletion Operation on Doubly Linear
Linked list
• Deleting a node from the beginning of the list
• Deleting a node from the end of the list
• Deleting a specified node.
Deleting a node from the beginning of
the Doubly Linear Linked list
1. Check the list is empty or not.
if(pfirst==NULL)
print list is empty and exit

2. Else if there is single node in a list


if(pfirst->next==NULL)
a. free the memory occupied by first node
free(pfirst)
b. set pfirst=NULL
plast=NULL

3. Else
a. store the address of second node in the list in temporary address “ptemp”
ptemp=pfirst->next
b. free the memory occupied by first node
free(pfirst)
c. And make the second node pointed by ptemp as first node and make the prev field of first node point to
NULL
pfirst=ptemp
pfirst->prev=NULL
4. Exit
Deleting a node from the beginning of the list
11. Copy the next field of first node
pfirst to the pointer ptemp ptemp

ptemp plast
pfirst

NULL

Free the memory occupied by first node


pfirst plast
ptemp

Make the second node as first node


pfirst plast

NULL
NULL
Deleting a node from the end of the list
1. Check the list is empty or not.
if(pfirst==NULL)
print list is empty and exit

2. Else if there is single node in a list


if(pfirst->next==NULL)
a. free the memory occupied by first node
free(pfirst)
b. set pfirst=NULL
plast=NULL

3. Else
a. store the memory address of second last node in temporary pointer ptemp
ptemp=plast->prev
b. free the memory occupied by last node
free(plast)
c. And make the address field of second last node point to NULL
ptemp->next=NULL
d. Make the second last node as last node
plast=ptemp
4. Exit
Deleting a node from the end of the list

1. Make the second last node to be pointed by “ptemp”


pfirst plast
ptemp
NULL

2. Free the memory occupied by the last node


plast
pfirst ptemp
NULL

3. Make the next field of second last node pointed by “ptemp” to NULL and make the second last node as
last node pfirst plast
NULL
Deleting the specified node
1. Input the specified location “loc” of a node to be deleted
2. Initialize the new pointer “pthis” with “pfirst”and move the pointer
“pthis” until it points to the specified node
pthis=pfirst
for(i=1;i<=loc-1;i++)
pthis=pthis->next
3. Store the address of the node just after and before the node to be
deleted in a temporary memory “ptemp1”, “ptemp” respectively
ptemp1=pthis->next
ptemp=pthis->prev
4. Make the next field of node pointed by pointer “ptemp” point to the
node pointed by ptemp1 and make the prev field of node pointed by
pointer “ptemp1” point to the node pointed by ptemp
ptemp->next=ptemp1
ptemp1->prev=ptemp
5. Free the memory occupied by the node to be deleted
free(pthis)
Deleting the specified node
1. Move the pointer “pthis” to the specified node . let the specified
node be Node3
pfirst pthis

Node1 Node2 Node3 Node4


2. Make the next field of node pointed by “ptemp” pointer point to the node just after the specified node and make the prev field
of node pointed by ptemp1 point to the just before the specified node
ptemp ptemp1
pfirst
NULL

3. Free the memory occupied by the node to be deleted


pfirst
NULL

You might also like