C Program for Insertion at end in doubly linked list

Insertion at the beginning of the linked list

Today we will discuss C program for Insertion at beginning in doubly linked list in which we have to insert node at the end of the doubly linked list.The doubly linked list have three field such as the node have the data, pointer which points next node in series and pointer which points previous node of the list.It is quite complicated than singly linked list but we can traverse linked list in both the direction left to right and vice-versa.
For Example :- Input : 12 54 3 7 66
Enter the data to insert at the end of the linked list : 70
Output : 12 54 3 7 66 70

Insertion at end in doubly linked list using C

Steps required for insertion at the end

  1.  Take a new_node and add new_node with data has to be inserted at the end of the linked list.
  2.  Then next ptr of the new_node is instanced of the last node.
  3.  The next pointer of the new_node is instanced to null and the prev pointer is instanced to the last node.
  4.  After that the new_node is made as the last node of the linked list.
  5.  Return new_node of the linked list.

Creating the structure of node in the doubly linked list :-

Code :

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

Algorithm for inserting an element at the end of the list :-

Step 1 :  NOW, 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 EXTRA = START
Step 7 :  (REPEAT STEP 8)WHILE EXTRA = EXTRA !->NULL
Step 8 :  ASSIGN EXTRA = EXTRA ->NEXT
Step 9 :  ASSIGN EXTRA ->NEXT = NEW NODE
Step 10 :  ASSIGN NEW NODE -> PREV = EXTRA AND RETURN

C program for Insertion at end 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 insert_End (int data)      // Function to insert node at the beginning of the doubly linked list
{
  struct node *new_node;
  if (last == NULL)
    {
      printf ("Please enter data for node \n");
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      new_node->next = NULL;
      new_node->prev = last;
      last->next = new_node;
      last = new_node;		// Make the new node as last node 
    }
}

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 for node1 : ");
      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 ("Enter 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 *extra;
  int n = 1;
  if (head == NULL)
    {
      printf ("\nList is empty\n");
    }
  else
    {
      extra = head;
      printf ("The doubly Linked List is here :\n");
      while (extra != NULL)	// Print the list
	{
	  printf ("%d   ", extra->data);
	  n++;
	  extra = extra->next;	// Move the current pointer to next node 
	}
    }
}

int main ()
{
  int n, data;
  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 insertion at the end of the list :\n ");
  scanf ("%d", &data);
  insert_End (data);
  print_List ();
  return 0;
}
Output :-
Enter the size of linked list : 
6
Enter data for node1 : 25
Enter data for node2 : 17
Enter data for node3 : 30
Enter data for node4 : 47
Enter data for node5 : 95
Enter data for node6 : 85
The doubly Linked List is here :
25  17  30  47  95  85
Enter data for insertion at the end of the list :
100
The doubly Linked List is here :
25  17  30  47  95  85  100