C Program for Insertion at end in doubly linked list

Insertion at the end of the linked list

Today we will discuss C program for Insertion at end 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 :-

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

struct Node
{
  int data;
  struct Node *next;
  struct Node *prev;
};

void insertLast (struct Node **head, int data)
{

  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

  newNode->data = data;
  newNode->next = NULL;

  //need this if there is no node present in linked list at all
  if (*head == NULL)
    {
      *head = newNode;
      newNode->prev = NULL;
      return;
    }

  struct Node *temp = *head;

  while (temp->next != NULL)
    temp = temp->next;

  temp->next = newNode;
  newNode->prev = temp;
}

void insertStart (struct Node **head, int data)
{

  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

  newNode->data = data;
  newNode->next = *head;
  newNode->prev = NULL;

  //If the linked list already had atleast 1 node
  if (*head != NULL)
    (*head)->prev = newNode;
  // *head->prev = newNode; would not work it has (*head) must be used

  //changing the new head to this freshly entered node
  *head = newNode;
}

// function to print the doubly linked list
void display (struct Node *node)
{
  struct Node *end;
  printf ("List in Forward direction: ");

  while (node != NULL)
    {
      printf (" %d ", node->data);
      end = node;
      node = node->next;
    }

  printf ("\nList in backward direction: ");

  while (end != NULL)
    {
      printf (" %d ", end->data);
      end = end->prev;
    }
}

int main ()
{
  struct Node *head = NULL;

  /*Need & i.e. address as we
     need to change head address only needs to traverse
     and access items temporarily
   */
  insertStart (&head, 12);
  insertStart (&head, 16);
  insertStart (&head, 20);

  insertLast (&head, 10);
  insertLast (&head, 14);
  insertLast (&head, 18);
  insertLast (&head, 11);
  /*No need for & i.e. address as we do not
     need to change head address
   */
  display (head);
  return 0;
}

Output

List in Forward direction:  20  16  12  10  14  18  11 
List in backward direction:  11  18  14  10  12  16  20

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Doubly Linked List

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java