Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C Program for Deletion at the end of the Singly Linked List

Deletion at the end of the Singly Linked List

Today we will learn how to delete head of the singly linked list which is quite easy operation as compared to the other operation applied on the singly linked list such as:-
->Deletion at the beginning of the singly linked list
->Deletion at the nth node of the singly linked list
In this we will remove first node of the linked by performing certain operation and give head to the second node of the linked list.
We discuss this problem here in C Programming Language.

Deletion at the end of the Singly Linked List using C

Operation performed for Deletion in Singly Linked List as following:-

  • Deletion at the beginning of the Singly Linked List
  • Deletion at the end node of the Singly Linked List
  • Deletion at the nth node of the Singly Linked
C Program for deletion at the end of the Singly Linked List

Learn how to make deletion at the end of the Singly Linked List as shown:-

struct node *temp;

temp = start;
start = start->next;

free(temp);

Algorithm :-

Step 1: IF HEAD = NULL
Go to Step 8
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5
WHILE (PTR -> NEXT!= NULL)
Step 4: SET PREVIOUS_PTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF WHILE LOOP]
Step 6: SET PREVIOUS_PTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT

Steps required for deleting the node:-

  • Copy the address of last node i.e. head node to some temp variable say to Delete.
  • Move the head to the second node of the linked list i.e. head = head->next.
  • Disconnect the connection of first node to second node.
  • Free the memory occupied by the first node.

C Code for deletion at the end of the Singly Linked List:-

#include<stdio.h>
#include<stdlib.h>
     
    typedef struct nodetype
    { 
    	int info;
    	struct nodetype *next;
    }   node ;
     
    void linked_list(node **,int );
    void deletion_at_last(node **);
    void display(node *);
    void main()
    {
     	node *start;
    	int item,n,p;
    	start=NULL;
    	printf("Enter number of nodes :\n");
    	scanf("%d",&n);
     
    	for(p= 0 ;p< n ;p++)
    	{	
    	    printf("Enter item for node %d :\n",p+1);
    		scanf("%d",&item);
    	    linked_list(&start,item) ;
    	}
    	printf("The list is like that after inserting element :\n");
    	display(start);
    	printf("\nPress any key to delete first node");
    	deletion_at_last(&start);
    	printf("\nThe list after the deletion at first node is :\n");
    	display(start);
    }
     
    void linked_list(node **start,int item)
    { 
    	node *ptr,*last;
    	ptr =(node*)malloc(sizeof(node));
    	ptr->info = item ;
    	ptr->next = NULL;
    	if(*start == NULL)
    	*start = ptr;
     	else
    	{ 
      		last = *start;
    		while(last->next !=NULL)
    		{ 
    			last = last->next;
    		}
    		last->next = ptr ;
    	}
     }
        void deletion_at_last(node **start)
        { 
    	    node *ptr,*cptr;
    	    int temp;
    	    if((*start)->next == NULL)
    	    { 
    		    ptr = *start ;
    		    *start = NULL;
    		    temp = ptr->info;
    	    }
    	    else
    	    { 
    		    cptr = *start ;
    		    ptr =(*start)->next;
    		    while(ptr->next != NULL)
    		    { 
    			    cptr = ptr;
    			    ptr = ptr->next;
    		    }
    		    cptr->next = NULL;
    		    temp = ptr->info;
    	    }
    	free(ptr);
     
    	printf("\nDeleted node item is = %d",temp);
     
    }
    void display(node *start)
    {   
        int n = 0;
    	while(start !=NULL)
        { 
          printf("\t %d",start->info);
          n++;
    	  start = start->next;
        }
      printf("\nTotal number of nodes : %d",n);
    }
Output:-
Enter number of nodes :
5
Enter item for node 1 :
41
Enter item for node 2 :
12
Enter item for node 3 :
32
Enter item for node 5 :
66
Enter item for node 6 :
30
The list is like that after inserting element :
41   12   32   66   30
Total number of nodes : 5
Press any key to delete first node
Deleted node item is = 30
The list after the deletion at first node is :
41   12   32   66 
Total number of nodes : 4