C Program for Deletion from nth position in doubly linked list

Deletion from particular position in doubly linked list

In this article we will learn C Program for Deletion from nth position in doubly linked list, the task is to delete the specific node by the user input.Here we have given algorithm ,working and code to understand better .
For Example :-
Input : 61   4   77   10   32
Enter the position from where you want to delete node : 3

Output : 61   4   10   32

Deletion from nth position in doubly linked list in C

Working needed for deleting node from the nth node in the doubly linked list :-

  • Make a new doubly linked list.
  • Then copy the address of head pointer into a temporary pointer .
  • Traverse the linked list until we find the desired position.
  • After that we have to make the next pointer of this node point to null so that it can be the new last node of the list.

 

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 the nth node in a Doubly Linked List

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

  • Step 1 : IF HEAD = NULL (GOTO STEP 9 AND END OF IF LOOP)
  • Step 2: NOW ASSIGN EXTRA = HEAD
  • Step 3: REPEAT STEP 4 WHILE TEMP → DATA != VALUE
  • Step 4: ASSIGN EXTRA = EXTRA → NEXT
  • Step 5: ASSIGN POINTER = EXTRA → NEXT
  • Step 6: ASSIGN EXTRA → NEXT = POINTER → NEXT
  • Step 7: ASSIGN POINTER → NEXT → PREVIOUS = EXTRA
  • Step 8: REMOVE POINTER
  • Step 9: EXIT

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

#include <stdio.h> 
#include <stdlib.h> 
struct node
{				// node structure
  int data;
  struct node *nextpt;
  struct node *prevpt;
} *lastpt, *headpt;
void delete_At_Pos(int pos)
{
  struct node *extra;
  int i,cur;
  extra = headpt;
  for (i = 1; i < pos && cur != NULL; i++) { extra = extra->nextpt;
    }
  if (extra != NULL)
    {
      extra->prevpt->nextpt = extra->nextpt;
      extra->nextpt->prevpt = extra->prevpt;
      free (extra);		// remove the extra node
    }
  else
    {
      printf ("Enter valid position!\n");
    }
}
void list(int n)
{
  struct node *new_node;
  int data, l;
  if (n >= 1)			// list is empty
    {
      headpt = (struct node *) malloc (sizeof (struct node));
      printf ("Enter the data of node : ");
      scanf ("%d", &data);
      headpt->data = data;
      headpt->prevpt = NULL;
      headpt->nextpt = NULL;
      lastpt = headpt;
      for (l = 2; l <= n; l++)
	{
	  new_node = (struct node *) malloc (sizeof (struct node));
	  printf ("Enter the data of node : ", l);
	  scanf ("%d", &data);
	  new_node->data = data;
	  new_node->prevpt = lastpt;
	  new_node->nextpt = NULL;
	  lastpt->nextpt = new_node;
	  lastpt = new_node;
	}
    }
}
void print()       // Print the linked list
{
  struct node *extra;
  int n = 1;
  if (headpt == NULL)
    {
      printf ("List is empty");
    }
  else
    {
      extra = headpt;
      while (extra != NULL)	
	{
	  printf ("%d  ", extra->data);
	  n++;
	  extra = extra->nextpt;
	}
    }
}
int main ()
{
  int n, pos;
  headpt = lastpt = NULL;
  printf ("Enter the size of nodes: ");
  scanf ("%d", &n);
  list (n);
  printf ("The Doubly Linked List is :\n");
  print ();

  printf ("\nEnter the position : ");
  scanf ("%d", &pos);
  delete_At_Pos (pos);
  printf ("After Deleting the node in the Linked List is :\n");
  print ();
  return 0;
}
Output:-
Enter the size of nodes: 6
Enter the data of node : 12
Enter the data of node : 1
Enter the data of node : 10
Enter the data of node : 9
Enter the data of node : 3
Enter the data of node : 25
The Doubly Linked List is :
12   1    10    9    3    25
Enter the position : 2
After Deleting the node in the Linked List is :
12    10    9    3    25