C program to remove the head node or first node from a linked list

C program to remove the head node or first node from a linked list :

In this C programming tutorial, we will learn how to remove the first node or first item from a linked list. Linked lists are ordered set of elements.Each element is known as a node. Each node contains one or more values and the address of the next node. In the C program below, we are first adding elements or nodes to a linked list. Next, we are deleting the first node from the list. Let’s take a look at the below image to learn how nodes are linked to each other :

How to delete a node :

Main algorithm to delete a node from a linked list is as below :

  1. The head pointer contains the address of the first node of the list. We need to remove this node. So, first store the content of the head pointer or the address of the first node in a different pointer variable, let’s say, temp.
  2. Now, change the head pointer value to point to the second node. After the first node will be deleted, the second node will become first.
  3. Finally, delete the first node. Now, the first node is deleted and the second node becomes the first. Means, the head is pointing to the first element of the linked list.

C program to delete a node from a list

#include<stdio.h>
#include<stdlib.h>

//1
typedef struct node{
  int value;
  struct node *next;
}node;

//2
void iterateList(node *head){
    while(head != NULL){
        printf("%d \n",head->value);
        head = head -> next;
    }
}

int main(){
  int length,i;

    //3
  printf("Enter size of the list : ");
  scanf("%d",&length);

    //4
  struct node *headNode, *currentNode, *temp;

    //5
  for(i=0 ; i<length; i++){
    currentNode = (node *)malloc(sizeof(node));

    printf("Enter element %d : ",(i+1));
    scanf("%d", &currentNode->value);

    if(i==0){
      headNode = temp = currentNode;
    }else{
      temp->next = currentNode;
      temp = currentNode;
    }
  }

    //6
  temp->next = NULL;

    //7
  printf("Iterating through the elements of the linked list : \n");
    iterateList(headNode);

    //8
    printf("Removing the first element...\n");
    
    temp = headNode;
    headNode = headNode->next;
    free(temp);
    
    //9
    printf("After first element is removed : \n");
    iterateList(headNode);
}

Explanation :

The commented numbers in the above program denote the step number below :

  1. Create one structure.This will act as the node of the linked list. It can hold one integer value and address to the next node of the same structure type. The name of this structure is node here.
  2. iterateList method is used to iterate through a linked list. We need to pass the address of the first node of a linked list to this method. It will iterate through each node and print out the values.
  3. At first, ask the user to enter the size of the linked list. Read the value and store it in length variable.
  4. Create three variables headNode, currentNode, and temp. These values can store the address of a node.
  5. Using a for loop, insert all values to the linked list.
  6. Set the address holding by the last node as NULL,i.e. it is the end of the linked list.
  7. Iterate through the elements of the linked list using the method iterateList and print out the values.
  8. This part is used to remove the first element of the linked list. We are using the same steps as explained in the Algorithm section above.
  9. Finally, print out the contents of the linked list one more time. This time the first element should not be there.

Sample Output :

Enter size of the list : 5
Enter element 1 : 8
Enter element 2 : 5
Enter element 3 : 6
Enter element 4 : 3
Enter element 5 : 1
Iterating through the elements of the linked list :
8
5
6
3
1
Removing the first element...
After first element is removed :
5
6
3
1


Enter size of the list : 3
Enter element 1 : 9
Enter element 2 : 1
Enter element 3 : 2
Iterating through the elements of the linked list :
9
1
2
Removing the first element...
After first element is removed :
1
2