You are on page 1of 21

Data Structures

Topic: Deletion from Linked List

By
Ravi Kant Sahu
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Deletion from a Linked List

• Deletion Algorithm
– Deleting the Node following a given Node
– Deleting a Node with a given ITEM of Information

• Review Questions

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

 A node N is to be deleted from the Linked List.


• Node N is between node A and node B.

 Deletion occurs as soon as the next pointer field of node A is


changed so that it points to node B.

 Types of Deletion:
• Deleting the node following a given node
• Deleting the Node with a given ITEM of Information.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deletion from Linked List

START

ITEM Ø

Node A Node N Node B

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Maintaining the AVAIL List
• After the deletion of a node from the list, memory space of
node N will be added to the beginning of AVAIL List.

• If LOC is the Location of deleted node N:

LOC->LINK = AVAIL
AVAIL = LOC

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deleting the Node Following a given Node
DEL (INFO, LINK, START, AVAIL, LOC, LOCP)

1. If LOCP = NULL, then:


       Set START = START->LINK. [Delete First node.]
Else:
       Set LOCP->LINK = LOC->LINK. [Delete node N.]
[End of If Structure.]  

2. [Return Deleted node to the AVAIL list]


Set  LOC->LINK = AVAIL and AVAIL= LOC.

3. EXIT.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Deleting the Node with a given ITEM of
Information
DELETE (INFO, LINK, START, AVAIL, ITEM)
1. Call FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)
            [Find the Location of node N and its preceding node]
2. If LOC = NULL, then: Write: ITEM not in LIST and EXIT.

3. [Delete node].
If LOCP = NULL, then:
Set START =  START->LINK. [Delete First node]
Else:
       Set LOCP->LINK = LOC->LINK.
[End of If Structure.]  

4. [Return Deleted node to the AVAIL list]


Set  LOC->LINK = AVAIL and AVAIL= LOC.
5. EXIT.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)

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


Set LOC = NULL, LOCP = NULL and Return.
[End of If Structure.]

2. [ITEM in First node?] If  START->INFO = ITEM, then:


Set LOC = START, and LOCP = NULL, and Return.
[End of If Structure.]

3. Set SAVE = START and PTR = START->LINK.  [Initializes pointers]


4. Repeat step 5 and 6 while PTR ≠ NULL.
5.           If PTR->INFO = ITEM, then: 
Set LOC = PTR and LOCP = 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 = NULL. [Search Unsuccessful.]
8. Return.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Exercise
• Write an algorithm to delete the last node of a linked list.
[Hint: Find out the address of last node and second last node]

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

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Header Linked List
 A header linked list which always contains a special
node, called the header node, at the beginning of the
list.

START
Ø

HEADER NODE

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

Name Salary LINK


2 0
START 1
5 59,000 6
2
E 10000 9
3
10 C 4000 7
AVAIL 4
8
5
B 20000 4
6
D 13000 3
7
1
8
F 12000 2
9
5
10
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Advantages of Header Linked List
• Header linked list contains a special node at the top.

• This header node need not represent the same type of data that
succeeding nodes do.

• It can have data like, number of nodes, any other data...

• Header node can access the data of all the nodes in the linked
list.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Types of Header Linked List
 A Grounded header list is a header list where the last node
contains the null pointer.

 A Circular header list is a header list where the last node


points back to the header node.

Note:
• Unless otherwise stated or implied, header list will always be circular
list.
• Accordingly, in such a case, the header node also acts as a sentinel
indicating the end of the list.

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

HEADER NODE

Grounded Header List

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

HEADER NODE

Circular Header List

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• If START->LINK = NULL,
then, Grounded Header List is Empty.

• If START->LINK = START,
then, Circular Header List is Empty.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Traversing a Circuar Header List
 Algorithm (Traversing a Circular Header list)

1. Set PTR = START->LINK. [Initialize pointer PTR] 


2. Repeat step 3 and 4 while PTR ≠ START.
3.              Apply PROCESS to PTR->INFO.
4.              Set PTR = PTR->LINK.   [PTR points to next node]
[End of Step 2 Loop.]
5. EXIT

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Use of Header Linked List
• Header Linked lists are frequently used for maintaining
Polynomials in memory.

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?

• Which pointer fields are changed when:


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

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

You might also like