2.4 Deletion of a node from single linked list.pptx
Sujan527908
9 views
11 slides
Sep 03, 2023
Slide 1 of 11
1
2
3
4
5
6
7
8
9
10
11
About This Presentation
Data structures
Size: 361.18 KB
Language: en
Added: Sep 03, 2023
Slides: 11 pages
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 HEADERLINK = 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 = ptrLINK 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 ptr1LINK = ptrLINK returnnode ( ptr ) exit endif endwhile Stop