Quantcast
Channel: C Programming Archives - QnA Plus
Viewing all articles
Browse latest Browse all 93

C Program to Delete the First Node of a Linked List

$
0
0

Here we’ll see how write C program to delete the first node of a linked list. Head always points to the first node. Head will move to where the first node is currently pointing to. First node can either point to the second node or to NULL in case the first node is the only node in the linked list. After moving the head, we can free up the memory of the first node.

Logic to Delete First Node of a Linked List.

  1. If head is NULL, return. There is no element in the linked list.
  2. Assign the head pointer to a temporary variable, tmp.
  3. Move the head to the next node. If the linked list has only one node, head will move to NULL.
  4. Delete the temporary pointer, tmp. As tmp is pointing to the first node, first node will be deleted.
Delete the first node of a linked list

Read also: Delete any node from a linked list.

The Program

/*File: test.c*/

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

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

/*Delete the first node of a linked list.*/
void delete_first_node(struct node **head) {
  struct node *tmp;

  /*Linked list does not exist or the list is empty*/
  if(head == NULL || *head == NULL) return;
  
  /*Storing the head to a temporary variable*/
  tmp = *head;
  
  /*Moving head to the next node*/
  *head = (*head)->next;
  
  /*Deleting the first node*/
  free(tmp);
}

/*Print the linked list*/
void print_list(struct node *head) {
    
    printf("H->");

    while(head)
    {
        printf("%d->", head->val);
        head = head->next;
    }

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

/*Insert an element at the front of the list*/
void insert_front(struct node **head, int value) {
    
    struct node * new_node = NULL;

    /*Allocating memory for the new node*/
    new_node = (struct node *)malloc(sizeof(struct node));

    if (new_node == NULL)
    {
        printf("Failed to insert element. Out of memory");
    }

    new_node->val = value;

    /*Pointing the new node to where head is currently pointing to*/
    new_node->next = *head;

    /*Pointing head to new node.*/
    *head = new_node;
}

void main()
{
    int count = 0, i, val;
    struct node * head = NULL;

    printf("Enter number of elements: ");
    scanf("%d", &count);

    for (i = 0; i < count; i++)
    {
        printf("Enter %dth element: ", i);
        scanf("%d", &val);
        insert_front(&head, val);
    }

    printf("Initial Linked List: ");
    print_list(head);
    
    delete_first_node(&head);
    
    printf("Linked List after first node deleted: ");
    print_list(head);
}

The above program first creates a linked list and then calls the delete_first_node() to delete the first node.

We passed head as double pointer to the delete_first_node() function because head will get changed from inside the function. To change a pointer from a function we need to pass that as double pointer.

Here is the output.

Delete first node of a linked list output

The post C Program to Delete the First Node of a Linked List appeared first on QnA Plus.


Viewing all articles
Browse latest Browse all 93

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>