C Program for Deletion from beginning in doubly linked list

Deletion from starting node in doubly linked list

In this article we will learn C Program for Deletion from beginning in doubly linked list, the task is to delete the first node of the doubly linked list and assign head to the second node.In the doubly linked list we can traverse in both direction because it has two pointer next and previous to move in both directions.
For Example :-
Input : 61 4 77 10 32
Output : 4 77 10 32

Deletion from beginning in doubly linked list in C

Implementation needed for deleting node from the starting in the doubly linked list :-

  • Firstly we have to delete the head node (start or first node) and copy the head node in a extra node.
  • And the task is make the second node as the head node.
  • Lastly we have to do is delete(free) the extra node.

How to made node Structure in the doubly linked list :-

struct node   
{  
    struct node *prev;   
    int data;  
    struct node *next;   
}  
C Program for Deletion from beginning in Doubly Linked list

Algorithm require for deleting node from the beginning in the linked list :-

  • STEP 1: NOW IF HEAD = NULL (GO TO STEP 6)
  • STEP 2: ASSIGN POINTER = HEAD
  • STEP 3: ASSIGN HEAD = HEAD → NEXT
  • STEP 4: ASSIGN HEAD → PREVIOUS = NULL
  • STEP 5: THAN FREE OR DELETE POINTER
  • STEP 6: EXIT OR RETURN

Did u know other two types of deletion click below :-

C Program for Deletion from beginning in doubly linked list :-

#include <stdio.h>
#include <stdlib.h>
struct node
{				// node structure
  int data;
  struct node *nextpt;
} *headpt;

void print ()
{
  struct node *extra;
  if (headpt == NULL)
    {
      printf ("List is empty");
    }
  else
    {
      extra = headpt;
      while (extra != NULL)	// Print the linked list
	{
	  printf ("%d  ", extra->data);
	  extra = extra->nextpt;
	}
      printf ("\n");
    }
}

void list (int n)
{
  struct node *new_node, *extra;
  int data, l;
  headpt = (struct node *) malloc (sizeof (struct node));
  if (headpt == NULL)		// list is empty
    {
      printf ("list is empty");
    }
  else
    {
      printf ("Enter the data of node : ");
      scanf ("%d", &data);
      headpt->data = data;
      headpt->nextpt = NULL;
      extra = headpt;
      for (l = 2; l <= n; l++)
	{
	  new_node = (struct node *) malloc (sizeof (struct node));
	  if (new_node == NULL)
	    {
	      printf ("list is empty");
	      break;
	    }
	  else
	    {
	      printf ("Enter the data of node : ", l);
	      scanf ("%d", &data);
	      new_node->data = data;
	      new_node->nextpt = NULL;
	      extra->nextpt = new_node;
	      extra = extra->nextpt;
	    }
	}
    }
}

void delete_At_Starting ()		// Function for deletion at the beginning 
{
  if (headpt == NULL)		// list is Empty
    printf ("list is Empty\n");
  struct node *extra;
  extra = headpt;
  headpt = headpt->nextpt;
  free (extra);			// free node
}

int main ()
{
  int n, data, pos;
  printf ("Enter the size of nodes: ");
  scanf ("%d", &n);
  if (n == 0)
    {
      printf ("List is empty\n");
      exit (0);
    }
  else
    {
      list (n);
    }
  printf ("The Doubly Linked List is :\n");
  print ();
  delete_At_Starting ();
  printf ("After Deleting the starting node the Linked List is :\n");
  print ();
  return 0;
}
Output:-
Enter the size of nodes: 6
Enter the data of node : 25
Enter the data of node : 41
Enter the data of node : 10
Enter the data of node : 63
Enter the data of node : 99
Enter the data of node : 10
The Doubly Linked List is :
25  41  10  63  99  10
After Deleting the starting node the Linked List is :
41  10  63  99  10