Linked List Insertion
Linked List Insertion
Types of insertion:
• Insertion at the beginning
• Insertion between two nodes
• Insertion at the end
Checking the Available List
AVAIL
ITEM Ø
Free-Storage List
NEW
Insertion at the beginning of Linked List
START
ITEM
3.INSERTION IN LINKED LIST
• Insertion into an unsorted linked list is generally
performed either at the beginning or at the end
of the list .In sorted linked list ,new data can be
inserted in the middle but the order has to be
maintained.
• To insert a new node into a linked list , we need
to first create space and then insert data in the
INFO part.
• The space for the new node is made available
from the free storage list or AVAIL list.
• If AVAIL=NULL(space is not available) no insertion
could be done
• If AVAIL !=NULL(Node available) then the first node
from the free storage list is removed and pointer
variable NEW is used to point to this new node.
• On removing the first node from the free storage
list,AVAIL pointer now points to next node in the
free storage list.
• Finally data is inserted into INFO(NEW)<-ITEM
3.1INSERTION AT THE BEIGNNING OF LIST
• INSTBEG(HEAD,ITEM,AVAIL)-Given LIST
first node address is stored in HEAD.
This algorithm inserts ITEM as the
first node in the linked list. Here we use
a local variable NEW that points to new
node and AVAIL pointer pointing to the
first node of free storage list
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.[make the new node’s link part points to first node of the list]
LINK(NEW)HEAD //as head contain address of first node,now new contain address of first node.
6.[Update HEAD pointer so that it points to new node]
HEADNEW
7.Return
3.2INSERTING NODE AFTER A GIVEN NODE
• We first create a new node from the free storage
list.
• We check the value of LOC,if LOC=NULL then new
node is inserted as the first node of list.
• If LOC !=NULL,we point the new node N to the
successor node X.The address of new node’s
successor can be found from node X’s link part.
• After this we set the link part of node X to now
point to the new node N..
• INSTLOC(HEAD,ITEM,AVAIL,LOC)-Given LIST first
node address is stored in HEAD.This algorithm inserts ITEM
either after the node whose location is LOC or inserts ITEM at
the front if LOC=NULL.Here we use a local pointer variable
NEW that points to a new node.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.If LOC =NULL then [Insert new node at the beginning]
LINK(NEW)HEAD
HEADNEW
Else [Inserts after node X having location LOC]
LINK(NEW)LINK(LOC) [Update link part of new node]
LINK(LOC)NEW [Update link part of node X to point to new node]
[End of If Structure]
6.Return
3.3 INSERTING A NODE AT THE END OF LIST
• Create new node from the free storage list and store ITEM
into INFO part of this node.
• LINK part must be set to NULL.
• We use a pointer variable PTR which is first intialized to
HEAD and then we navigate through the nodes in the list till
you get a node whose link part is NULL.
• INSTEND(HEAD,ITEM,AVAIL)-Given LIST’s first
node is stored in HEAD.This algorithm inserts ITEM
at the end of the list.Here we use two local pointer
variables NEW and PTR.The NEW points to the new
node.The PTR navigates the linked list so as to find
the location of the last node after which new node is
to be inserted.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return
[end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.LINK(NEW)NULL [Set link part of the new node to NULL]
6.[Searches for location of last node of linked list]
PTRHEAD [Intialize PTR to HEAD]
7.Repeat while LINK(PTR) !=NULL
PTRLINK(PTR)
[End of loop]
8.LINK(PTR)NEW [Set link part of PTR to point to new node]
9.Return.
3.4INSERTION IN THE SORTED LIST
INSTSORT(HEAD,ITEM,AVAIL)-Given LIST’s first
node address in contained in HEAD.This algorithm
inserts ITEM into sorted linked list based on the
value of ITEM.The local variable PTR points to the
current node being processed and pointer variable
PREVPTR points to its predecessor.The local variable
NEW points to the new node and AVAIL pointer
points to the first node in the free storage list.
1.If AVAIL=NULL then [Overflow ,Avail list empty]
Write “OVERFLOW”
return [end of IF structure]
2.NEWAVAIL
3.AVAILLINK(AVAIL) [Update AVAIL]
4.INFO(NEW)ITEM [copy ITEM into INFO part of new node]
5.LINK(NEW)NULL [Set link part of the new node to NULL]
6.[Is the list empty?]
If HEAD=NULL then
HEADNEW [set NEW as HEAD of new list]
Return [end of if structure]
7.[Traverse the list if non-empty]
PTRHEAD [set PTR to HEAD of the list]
8.PREVPTRNULL [set PREVPTR to NULL]
9.Repeat while PTR!=NULL and ITEM>INFO(PTR)
[Update pointers]
a)PREVPTRPTR
b)PTRLINK(PTR)
[End of step 9 loop]
10.[Does new node precede all nodes in list?]
If PREVPTR =NULL then [Insert new node at the front]
a)LINK(NEW)HEAD
b)HEADNEW
Else [Insert new node somewhere in the middle or at the end]
a)LINK(NEW)PTR
b)LINK(PREVPTR)NEW
[End of step 10]
11.Return.
Review Questions
• What is the condition for the list being empty?