2.4 Deletion of a node from single linked list.pptx

Sujan527908 9 views 11 slides Sep 03, 2023
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Data structures


Slide Content

Deletion from the single linked list

Removing a node from the linked list Deletion involves three cases : Case 1 : Delete the first node present in the list Case 2 : Delete the last node present in the list Case 3 : Delete any node present in the list

Delete the first node in the list Procedure: Check if the header points to NULL. If so, return “ no nodes present in the list. Deletion is impossible.” Otherwise : 1. Delete the first node by altering the LINK Fields 2. The header node link field points to the second node in the list.

Algorithm: Ptr = HEADER  LINK If ( Ptr = NULL ) then print “ The list is empty: No deletion” Exit Else Ptr1= ptr LINK HEADERLINK = Ptr1 ReturnNode ( Ptr ) Endif Stop

Delete the last node: Procedure: Check if the header points to NULL. If so, return “ no nodes present in the list. Deletion is impossible.” Otherwise : 1. Delete the last node by altering the LINK Fields 2. Take a variable called ptr1, store the current position in ptr and compare the current node pointer points to NULL Value. 3. Remove the last node.

Algorithm: Ptr = HEADER  LINK If ( Ptr = NULL ) then print “ The list is empty: No deletion” Exit Else While ( ptr  LINK != NULL) do ptr1= ptr ptr = ptrLINK endwhile ptr1 LINK=NULL endif stop

Delete any node: ptr1=HEADER If(ptr1=NULL) print “ no nodes in the list, deletion not possible” Else ptr =ptr1  LINK while( ptr != NULL) do if( ptr  DATA != KEY) then ptr1= ptr ptr = ptr LINK else ptr1LINK = ptrLINK returnnode ( ptr ) exit endif endwhile Stop
Tags