Insertion at Beginning in Doubly Linked list in C++

How to insert element at beginning in doubly linked list?

In singly linked list, we can move only in  single direction because each node has the address of the next node only.But in doubly linked list  there are two pointers, one of these pointers points to the next node and the other points to the previous node so we can move in both directions. A doubly linked list allow insertion at:-
  • Insertion at the end of the list.
  • Insertion in between the nodes.
  • Insertion at the beginning.
Insertion at beginning in doubly linked list in C++

Doubly link list definition in C++

struct Node 
{
  int Data;
  Struct Node* next;
  Struct Node* prev;
};

Steps to insert element at beginning in doubly linked list in C++

1.) Allocate node.

2.)Put the data.

3.) Make next node as head and previous as null.

4.)Change previous of head node to the new node.

5.)Move the head to point the new node.

Insertion at beginning in doubly linked list in C++

Algorithm for insertion at beginning in doubly linked list in C++

  • IF ptr = NULL
  • SET NEW_NODE = ptr
  • SET ptr = ptr -> NEXT
  • SET NEW_NODE -> DATA = VAL
  • SET NEW_NODE -> PREV = NULL
  • SET NEW_NODE -> NEXT = START
  • SET head -> PREV = NEW_NODE
  • SET head = NEW_NODE
  • EXIT

Program for insertion at beginning in doubly linked list in C++

Run
#include<iostream>
using namespace std;

struct node
{
  int num;
  struct node *preptr;
  struct node *nextptr;
} *stnode, *ennode;


void DlListcreation (int n);
void DlLinsertNodeAtBeginning (int num);
void displayDlList ();

int main ()
{
  int n, num1;
  stnode = NULL;
  ennode = NULL;

  cout << "Enter the number of nodes : ";
  cin >> n;
  DlListcreation (n);
  displayDlList ();
  cout << " Input data for insertion at beginning : ";
  cin >> num1;
  DlLinsertNodeAtBeginning (num1);
  displayDlList ();
  return 0;
}

void DlListcreation (int n)
{
  int i, num;
  struct node *fnNode;

  if (n >= 1)
    {
      stnode = (struct node *) malloc (sizeof (struct node));

      if (stnode != NULL)
	{
	  cout << " Input data for node 1 : ";	// assigning data in the first node
	  cin >> num;

	  stnode->num = num;
	  stnode->preptr = NULL;
	  stnode->nextptr = NULL;
	  ennode = stnode;
	  for (i = 2; i <= n; i++)
	    {
	      fnNode = (struct node *) malloc (sizeof (struct node));
	      if (fnNode != NULL)
		{
		  cout << " Input data for node " << i << ": ";
		  cin >> num;
		  fnNode->num = num;
		  fnNode->preptr = ennode;	// new node is linking with the previous node
		  fnNode->nextptr = NULL;

		  ennode->nextptr = fnNode;	// previous node is linking with the new node
		  ennode = fnNode;	// assign new node as last node
		}
	      else
		{
		  cout << " Memory can not be allocated.";
		  break;
		}
	    }
	}
      else
	{
	  cout << " Memory can not be allocated.";
	}
    }
}

void DlLinsertNodeAtBeginning (int num)
{
  struct node *newnode;
  if (stnode == NULL)
    {
      cout << " No data found in the list\n";
    }
  else
    {
      newnode = (struct node *) malloc (sizeof (struct node));
      newnode->num = num;
      newnode->nextptr = stnode;	// next address of new node is linking with starting node
      newnode->preptr = NULL;	// set previous address field of new node is NULL
      stnode->preptr = newnode;	// previous address of starting node is linking with new node
      stnode = newnode;		// set the new node as starting node
    }
}

void displayDlList ()
{
  struct node *tmp;
  tmp = stnode;
  int n = 1;
  while (tmp != NULL)
    {
      cout << " node " << n << ": " << tmp->num << "\n";
      n++;
      tmp = tmp->nextptr;	// current pointer moves to the next node
    }
}
Output:
 Enter the number of nodes : 5
 Input data for node 1 : 11
 Input data for node 2: 22
 Input data for node 3: 33
 Input data for node 4: 44
 Input data for node 5: 55

 Data entered in the list is :
 node 1: 11
 node 2: 22
 node 3: 33
 node 4: 44
 node 5: 55
 Input data for insertion at beginning : 1

 After insertion the new list is :
 node 1: 1
 node 2: 11
 node 3: 22
 node 4: 33
 node 5: 44
 node 6: 55

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

  • 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

Doubly Linked List