You are on page 1of 18

Session 105,106

Deletion in singly linked list

KLEF P-1(CTD) BES-1


Session Outcomes
Student will be able to:
1. Understand the delete operation in single linked list.
2. Perform delete operation in single linked list.

KLEF P-1(CTD) BES-1


Objectives
1. Design a step-by-step procedure to delete a node in
singly linked list
2. Develop a solution to delete node at various
locations of singly linked list

KLEF P-1(CTD) BES-1


deletion of a Node

1. at the start 3. at the given


node of the list location of the list

2. at the end node


of the list

KLEF P-1(CTD) BES-1


1. Steps to delete start node of SLL
head new link
ptr
100
20 200 30 300 40 null
Old link
100 300
200
ptr=head
head=ptr->next
delete ptr
SLL after deleting start node

head

200 30 300 40 null


200 300
Deletion of start node: Algorithm
Step 1: if head = null
Print “underflow”
     Go to Step 5
    [end of if]
Step 2: set ptr = head
Step 3: set head = head -> next
Step 4: free ptr
Step 5: exit

KLEF P-1(CTD) BES-1


Function for deleting start node in SLL
void deleteStartNode()  
    {  
        struct node *ptr;  
        if(head == NULL)  
        {  
            printf("\nList is empty");  
        }  
        else   
        {  
            ptr = head;  
            head = ptr->next;  
            free(ptr);  
            printf("\n start Node deleted ...");  
        }  
    }  
2. Deletion of last node
Two scenarios
1. There is only one node in the list and that
needs to be deleted.

2.There are more than one node in the list and


the last node of the list will be deleted.

KLEF P-1(CTD) BES-1


2. Deletion at the end node of SLL

ptr p

10 100 20 200 30 300 40 null

115 200 300


100

ptr->next=null

free(p)
SLL after deletion of end node

head

10 100 20 200 30 null

115 100 200


2.Deletion of last node: Algorithm
Step 1: if head = null
print underflow
   Go to Step 8
  [END OF if]
Step 2: Set ptr = head
Step 3: Repeat Steps 4 and 5 while ptr -> next!= null
Step 4: Set preptr = ptr
Step 5: Set ptr = ptr -> next
[end of loop]
Step 6: Set preptr -> next = null
Step 7: free ptr
Step 8: exit

KLEF P-1(CTD) BES-1


Function for deleting last node
void end_delete()    else  
    {           {  
        struct node *ptr,*ptr1;               ptr = head;   
        if(head == NULL)               while(ptr->next != NULL)  
        {                   {  
            printf("\n list is empty");                       ptr1 = ptr;  
        }                       ptr = ptr ->next;  
      else if(head -> next == NULL)                   }  
        {                   ptr1->next = NULL;  
          head = NULL;                   free(ptr);  
          free(head);               printf("\
          printf("\n n Deleted Node from the last ...");  
Only node of the list deleted ...");               }     
        }           }  
              
KLEF P-1(CTD) BES-1
3. deletion at the given location of SLL
new link

head

10 100 20 200 30 300 40 null


100 200 300

location=2
deletion at the middle node of SLL

head

20 200 40 null
300
100
3. Deletion of node at given position:
STEP 1: if head = null
Algorithm
print underflow
    goto step 10
   end of if
STEP 2: Set temp = head
STEP 3: set i = 0
STEP 4: repeat step 5 to 8 until i<loc
STEP 5: temp1 = temp
STEP 6: tempp = temp → next
STEP 7: if temp = null
print “node not present"
    goto step 12
    end of if
STEP 8: i = i+1
end of loop
STEP 9: temp1 → next = temp → next
STEP 10: free temp
STEP 11: exit
3. Deletion of node at given position
void delete_specified()   if(ptr == NULL)  
    {               {  
        struct node *ptr, *ptr1;              printf("\
        int loc,i;    nThere are less than %d elements i
        scanf("%d",&loc);   n the list..\n",loc);  
        ptr=head;                   return;  
        for(i=0;i<loc;i++)               }  
        {           }  
            ptr1 = ptr;                ptr1 ->next = ptr ->next;  
            ptr = ptr->next;           free(ptr);  
                 printf("\
nDeleted %d node ",loc);  
            
KLEF     }     
P-1(CTD) BES-1
Example: deleting middle node
Try !

KLEF P-1(CTD) BES-1

You might also like