c program to delete first node from singly linked list

By | January 14, 2017

Write ‘c’ program to create singly linked list of “n” elements and delete first node from the singly linked list ( delete head element ).

How to Delete a node from singly linked list

                                                                 Delete a node from singly linked list

Algorithm to delete first node from singly linked list.

Input: Pass Head of the Linked list

Begin:         

           If  ( head != NULL ) then

                nodeDelete =  head

                head = head-->head.next

                release_memory(nodeDelete)         

           End If

End
 

Steps to Delete node from Singly Linked list

  1. Store the Head Element Address into temporary Variable.Step 1 Delete first node from linked list                                              Step 1: Delete first node from linked list
  2.       Move the Head Element Pointer to Next Node Address.Step 2 Delete first node from linked list                                           Step 2: Delete first node from linked list
  3. Remove the Link between Existing Head Element and it’s next node Address.Step 3 Delete first node from linked list                                             Step 3: Delete first node from linked list
  4. Delete the memory ,which is used by the Head Element.Step 4 Delete first node from linked list                                              Step 4: Delete first node from linked list.

c/c++ Program to delete first node from singly linked list


#include <bits/stdc++.h>
#include <string.h>
using namespace std;
// Structure of node.It contains two elements.one is string.
// Second one is Pointer to Node

struct node
{
    int data;
    struct node* next;
};

/* Function to create a new node with given data */
node* newNode(int data)
{
   node *new_node = new node;
   new_node->data = data;
   new_node->next = NULL;
   return new_node;
}
node* GenerateLinkedList(int n)
{
   int data;
   int i;

   struct node *temp;
   struct node *headtemp;
   struct node *head;
   struct node *newlycreatednode;

   printf("Enter the data of first node:-\n\n");
   cin >> data;
   cout << endl << endl;
 
   head = newNode(data);
   temp = head;
   //this loop will be executed,if it is more than 2 nodes.
   for(i=2; i<=n; i++)
   {
     printf("Enter the data of %d node:-\n\n",i);
     cin >> data;
     cout << endl << endl;
     newlycreatednode = newNode(data);
     newlycreatednode->next = NULL;

     temp->next = newlycreatednode;
     temp = temp->next;
   }

   printf("Linked List Created with nodes - %d successfully \n\n",n);

   return head;//return reference to head element of the linked list.
}

void displayLinkedList(node *head)
{
   struct node *temp;

   if(head == NULL)
   {
      printf("LinkedList is empty.\n\n");
   }
   else
   {
     temp = head;
     printf("Displaying linked List.\n\n");

     while(temp != NULL)
     {
       cout<<temp->data<<"=====>";
       temp = temp->next;
     }

       printf("END\n\n");
   }
}

/* return pointer to new head node */

node* deleteFirstNode(node* head)
{
   struct node *temoporaryNode;

   if(head == NULL)
   {
      printf("Bro,Linkedlist is Empty.Nothing to Delete\n\n");
      return NULL;
   }
   else
   {

      temoporaryNode = head;
      head = head->next;


      printf("\nNode value to be deleted = %d\n\n", temoporaryNode->data);

      free(temoporaryNode);//Actual Memory Deletion is equal to node deletion.

      printf("DELETED MEMORY OF THE HEAD ELEMENT\n\n");

      printf("SUCCESSFULLY HEAD ELEMENT DELETED\n\n");

      return head;
   }
}

//Input Program to pass
//If we combine the all the characters in the String:Saippuakivikauppias
//Main program
int main()
{
     int n, position;
     int confirmation=0;

     /* It will create Linked List with given N nodes*/
     printf("Enter the total number of nodes to create: \n\n");
     scanf("%d", &n);

     node* head=GenerateLinkedList(n);//Head of the linked list is stored in "head".

     printf("Existing Linked List Elements as Below \n");

     //Display created linked list.
     displayLinkedList(head);

     printf("Want to Delete HEAD Element From Linked List,then ENTER <=1=> ? \n\n");
     scanf("%d",&confirmation);

     if(confirmation)
     {
        head = deleteFirstNode(head);
     }
     else
     {
         printf("Exiting the Current Program,without deletion of Head node of the linkedlist \n\n");
     }

     printf("Existing Linked List Elements After Deletion of Head Element \n\n");

     //Display created linked list.
     displayLinkedList(head);

     return 0;
}

 

Output with Compilation and End output Screen attached for the better understanding.

Program output 1 Program output 2

 

Hope you have enjoyed the End to End covering right from code to execution.Love to see you on you more articles in this blog.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply