Deletion of node from beginning(front) in SLL

Similar to the Front Insertion,Front deletion is also very simple ans easy implementation and very cheap operation :

  • First we have to create an HeadPinter and one Temp Variable Now we will initialize the first node address to the HeadPointer and Temp variable also.
  • Now we have to Assign the address of HeadPointer to HeadPointer Only and delete the Temp.

Working Process

Code Implementation

#include <stdio.h>
#include<math.h>
#include<stdlib.h>
struct CreateNode   // Node Structure Initialization
{
    int  data;          // for data element
    struct CreateNode* nextNode;   //for storing the address of next element
};
struct CreateNode *HeadPointer=NULL;    // Initialization of HeadPointer
void deleteNodeAtFront()   // Method of front Addition
{
    struct CreateNode* TempNode=NULL;  // Create one TempNode 
    TempNode=HeadPointer;
    HeadPointer=HeadPointer->nextNode;
    printf("%d is deleted from the front\n ",TempNode->data);
    delete TempNode;  // delete the front node or Memory deallocation of front node.
}
int main()
{
    int choice=0;
    //Create 4 node statically and Initialize manually
    struct CreateNode* Node1=(struct CreateNode *)malloc(sizeof(struct CreateNode *)); 
    struct CreateNode* Node2=(struct CreateNode *)malloc(sizeof(struct CreateNode *)); 
    struct CreateNode* Node3=(struct CreateNode *)malloc(sizeof(struct CreateNode *)); 
    struct CreateNode* Node4=(struct CreateNode *)malloc(sizeof(struct CreateNode *)); 
    Node1->data=90;
    Node2->data=100;
    Node3->data=200;
    Node4->data=300;
    
    Node1->nextNode=Node2;
    Node2->nextNode=Node3;
    Node3->nextNode=Node4;
    Node4->nextNode=NULL;
    
    HeadPointer=Node1;  // HeadPointer pointing to the Node1
    
    printf("Node data before deletion process :\n");
    while(HeadPointer->nextNode!=NULL)
    {
        printf("%d ",HeadPointer->data);
        HeadPointer=HeadPointer->nextNode;
    }
    printf("%d ",HeadPointer->data);
    HeadPointer=Node1;
    while(choice==0)
    {
        printf("\nPress 0 for front deletion :");
        scanf("%d",&choice);
        if(choice==0)
        {
            deleteNodeAtFront();
        }
        else
        {
            choice=1; // dont delete the front node
        }
    }
    
    printf("Node data after deletion process :\n");
    while(HeadPointer!=NULL)
    {
        printf("%d ",HeadPointer->data);
        HeadPointer=HeadPointer->nextNode;
    }

    return 0;
}

Time & Space Complexity

  • For the front Deletion , Complexity will be (n).
  • Space complexity will be O(1).

Leave a Reply

Your email address will not be published. Required fields are marked *