Professional Documents
Culture Documents
,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
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
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
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
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
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
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
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