You are on page 1of 13

Data Structures

Topic: Insertion in Linked List

By
Ravi Kant Sahu
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Insertion Algorithm
– Insertion at the beginning
– Insertion after a given node
– Insertion into a sorted list
• Review Questions

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion into a Linked List

 New node N (which is to be inserted) will come from AVAIL


list.
• First node in the AVAIL list will be used for the new node N.

 Types of insertion:
• Insertion at the beginning
• Insertion between two nodes

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Checking the Available List

AVAIL

ITEM Ø

Free-Storage List
NEW

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion at the beginning of Linked List

START

ITEM

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion Algorithm (Beginning of the list)

INSFIRST (INFO, LINK, START, AVAIL, ITEM)

1. [OVERFLOW?] If AVAIL = NULL, then: Write: OVERFLOW, and


Exit.
2. [Remove first node from AVAIL list]
Set  NEW= AVAIL and AVAIL= AVAIL->LINK.
3. Set NEW->INFO  = ITEM. [Copy the new data to the node].
4. Set NEW->LINK  = START. [New node points to the original first
node].
3. Set START = NEW. [START points to the new node.]
4. EXIT.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion after a given node
START

ITEM

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion Algorithm (After a given node)
INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)

1. [OVERFLOW?] If AVAIL = NULL, then: Write: OVERFLOW, and


Exit.
2. [Remove first node from AVAIL list]
Set  NEW= AVAIL and AVAIL= AVAIL->LINK .
3.    Set NEW->INFO  =  ITEM. [Copy the new data to the node].
4.    If LOC = NULL, then: [Insert as a first node]
       Set NEW->LINK  = START and START = NEW.
Else: [Insert after node with location LOC.]
       Set NEW->LINK  =  LOC->LINK  and LOC->LINK = NEW.
[End of If structure.]
5.     Exit.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Insertion into a sorted Linked List
• If ITEM is to be inserted into a sorted linked list. Then ITEM must be
inserted between nodes A and B such that:
A->INFO   <   ITEM    <    B->INFO
• First of all find the location of Node A
• Then insert the node after Node A.

INSERT (INFO, LINK, START, AVAIL, ITEM)

1. CALL FIND_A (INFO, LINK, START, AVAIL, ITEM)


[USE Algorithm FIND_A to find the location of node preceding
ITEM.] 
2. CALL INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)
[Insert ITEM after a given node with location LOC.] 
3. Exit.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
FIND_A (INFO, LINK, START, ITEM, LOC)

1. [List Empty?] If START = NULL, then: Set LOC = NULL, and


Return.
2. [Special Case?] If ITEM  <  START->INFO , then: Set LOC = NULL,
and Return. 
3. Set SAVE = START and PTR = START->LINK .  [Initializes pointers]
4. Repeat step 5 and 6 while PTR ≠ NULL.
5.           If ITEM <  PTR->INFO then: 
Set LOC = SAVE, and Return.
   [End of If Structure.]
6.     Set SAVE = PTR and PTR = PTR->LINK . [Update pointers]
[End of Step 4 Loop.]
7.    Set LOC = SAVE.
8.     Return.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Review Questions
• What is the condition for the list being empty?

• How will you insert a node in a linked list after a given


node?

• Which pointer fields are changed when:


– a node is inserted after a given node
– a node is inserted at the end of list
– a node is inserted at the beginning of the list.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Review Questions
• Correct the following algorithm such that ptr contains the
address of the last node:

1. Set PTR = START


2. Repeat Steps 3 WHILE PTR != NULL
3.    Set PTR->LINK  =  PTR.
[End of the Loop]
4. Return PTR.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like