C Program for Insertion at beginning in doubly linked list

Insertion at beginning in doubly linked list

In this section we will study C Program for Insertion at beginning in doubly linked list in which the task is to insert element at leftmost of the linked list.The doubly linked allow us to traverse in both the directions from left to right and right to left.Also in the doubly linked list every node of the linked list contain double pointers or links so that we have to maintain more number of pointers in doubly linked list as compared to the singly linked list.

For Example:- Input : 25 4 51 20 9
Enter the element to insert at the beginning of the linked list : 33
Output : 33 25 4 51 20 9

Insertion at beginning in doubly linked list using C

Steps used to inserting element in the doubly linked list:-

  • Take a new_node then we put new_node with data has to be inserted at the beginning of the linked list.
  • The next ptr of the new_node is instanced of the head node and the prev ptr is instanced to null.
  • The prev ptr of the head node is instanced to the new_node.
  • After that the new_node is made as the head node.
  • Return the new_node.

Learn how to create a struct node of a doubly linked list ->

Syntax :-

struct node   
{  
    struct node *prev;   
    int data;  
    struct node *next;   
}
C program for Insertion at beginning in doubly linked list

Algorithm for inserting element in the linked list:-

Step 1 : IF POINTER = NULL
Step 2 : ASSIGN NEW NODE = POINTER
Step 3 : ASSIGN POINTER = POINTER -> NEXT
Step 4 : ASSIGN NEW NODE -> DATA = VALUE
Step 5 : ASSIGN NEW NODE -> PREV = NULL
Step 6 : ASSIGN NEW NODE -> NEXT = START
Step 7 : ASSIGN HEAD -> PREV = NEW NODE
Step 8 : ASSIGN HEAD = NEW NODE
Step 9 : RETURN

C Program for Insertion at beginning in doubly linked list:-

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

struct node         //structure of doubly Node 
{
  int data;
  struct node *prev;
  struct node *next;
} *head, *last;

void list (int n)       //print list of nodes
{
  int i, data;
  struct node *new_node;
  if (n >= 1)
    {
      head = (struct node *) malloc (sizeof (struct node));
      printf ("Enter data of node 1 : ");
      scanf ("%d", &data);
      head->data = data;
      head->prev = NULL;
      head->next = NULL;
      last = head;
      for (i = 2; i <= n; i++) 
      { 
          new_node = (struct node *) malloc (sizeof (struct node)); 
          printf ("\nEnter data of node %d : ", i); 
          scanf ("%d", &data); 
          new_node->data = data;
	  new_node->prev = last;	// Now Link new node with the previous node
	  new_node->next = NULL;
	  last->next = new_node;	// Then Link previous node with the new node
	  last = new_node;	        // Let Make new node as last node
    }}
}

void print_List ()
{
  struct node *temp;
  int n = 1;
  if (head == NULL)
    {
      printf ("\nList is empty\n");
    }
  else
    {
      temp = head;
      printf ("The Doubly Linked List is :\n");
      while (temp != NULL)      // Print the list
	{
	  printf ("%d   ", temp->data);
	  n++;
	  temp = temp->next;        // Move the cur ptr to next node 
	}
    }
}
// Func to insert node at the beginning of the doubly linked list
void insert_Beginning (int data)
{
  struct node *new_node;
  if (head == NULL)
    {
      printf ("Please enter data for node \n");
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      new_node->next = head;
      new_node->prev = NULL;
      head->prev = new_node;	// Link prev add field of head with new_node
      head = new_node;		// Make the new node as head node 
    }
}

int main ()
{
  int n, data, choice = 1;
  head = last = NULL;
  printf ("Enter the size of linked list : \n");	// Input the size of nodes
  scanf ("%d", &n);
  list (n);
  print_List ();
  printf ("\nEnter data for insert at the beginning :\n ");
  scanf ("%d", &data);
  insert_Beginning (data);
  print_List ();
  return 0;
}
Output:-
Enter the size of linked list : 
5
Enter data of node 1 : 25

Enter data of node 2 : 12

Enter data of node 3 : 2

Enter data of node 4 : 65

Enter data of node 5 : 15
The Doubly Linked List is :
25  12  2  65  15
Enter data for insert at the beginning :
98
The Doubly Linked List is :
98  25  12  2  65  15