Insert node at beginning of Doubly linked list

That is very simple to add new created doubly linked list node at front (beginning) position. That are similar to single linked list but only the difference are this are contain two pointer fields. next and pervious pointer. next pointer are hold the reference of next node of linked list and previous pointer are hold the reference of previous node.

Insertion approach: There is two cases to add doubly linked list node at front position. First is when linked list are empty, and we are insert first element of linked list . In this case created newly doubly linked list node and set this node pointer fields to (next and previous) NULL. and assign this node address to head (head pointer) of linked list.

When linked list are not empty then create a doubly linked list node and this node next pointer are assign the address of first node of linked list (Note that head pointer are holds the reference of first node).

And this head node previous pointer field are assign newly created node and new created node next pointer is assign to head of linked list. after that provide this new node reference to head pointer.

Suppose we are inserted the following (80,70,60,50,40,30,20,10) node in a sequence.

insertion node at beginning

Here given code implementation process.

//C Program
//Insert node at beginning of doubly linked list

#include <stdio.h>
//For malloc function
#include <stdlib.h> 

//Create structure
struct Node
{
  int data;
  struct Node*next;
  struct Node*prev;
};

struct Node*head;

//Insert Node element
void insert(int value)
{
  //Create a dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));

  if(node==NULL)
  {
    printf("Memory overflow\n");
  }
  else
  {
    //Set data value
    node->data=value;
    node->next=head;
    
    node->prev=NULL;
    if(head!=NULL)
    {
      //When non empty list
      head->prev=node;
    }
    //newly node as head of linked list
    head=node;
  }
}
//Display element of Node
void display()
{

  if(head==NULL)
  {
    printf("Empty linked list");
  }
  else
  {
    printf("\nLinked List Elements : ");
    struct Node*temp=head;
    //Traverse doubly linked list from front to rear
    while(temp!=NULL)
    {
      //print node value
      printf("%d  ",temp->data);
      temp=temp->next;
    }
  }

}

int main()
{
  //set node pointer value
  head=NULL;
  //Insert element of linked list
  insert(80);
  insert(70);
  insert(60);
  insert(50);
  insert(40);
  insert(30);
  insert(20);
  insert(10);
  //display all node
  display();
  return 0;
}

Output

Linked List Elements : 10  20  30  40  50  60  70  80
//C++ Program 
//insert new node at beginning of doubly linked list
//Define class of linked list Node
#include<iostream>

using namespace std;
class Node {
  public:
  int data;
  Node *next;
  Node *prev;
  Node(int data) {
    this->data = data;
    this->next = NULL;
    this->prev = NULL;
  }
};
class LinkedList {
  public:
  Node *head;
  //Class constructors

  LinkedList() {
    this->head = NULL;
  }
  //insert node at beginning of linke list
  void insert(int value) {
    //Create a node
    Node *node = new Node(value);
    node->next = this->head;
    //when linked list is not empty

    if (this->head != NULL) {
      this->head->prev = node;
    }
    this->head = node;
  }
  //Display node element of doubly linked list
  void display() {
    if (this->head == NULL) {
      cout << "Empty Linked List";
    } else {
      cout << "Doubly Linked List Element :";
      Node *temp = this->head;
      while (temp != NULL) {
        cout << " " << temp->data;
        temp = temp->next;
      }
    }
  }
};
int main() {
  LinkedList obj =  LinkedList();
  //insert following linked list nodes
  obj.insert(80);
  obj.insert(70);
  obj.insert(60);
  obj.insert(50);
  obj.insert(40);
  obj.insert(30);
  obj.insert(20);
  obj.insert(10);
  obj.display();
  return 0;
}

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
//Java Program 
//insert new node at beginning of doubly linked list

//Define class of linked list Node
class Node
{
  public int data;
  public Node next;
  public Node prev;
  public Node(int data)
  {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}
public class LinkedList
{
 
  public Node head;

  //Class constructors
  LinkedList()
  {
    head = null;
  } 
  //insert node at beginning of linke list
  public void insert(int value)
  {
    //Create a node
    Node node = new Node(value);

    node.next = head;

    //when linked list is not empty
    if (head!=null) 
    {
      head.prev=node;
    }

    head = node;
    
  }
  //Display node element of doubly linked list
  public void display()
  {
    if(head==null)
    {
      System.out.println("Empty Linked List");
    }
    else
    {
      System.out.print("Doubly Linked List Element :");

      Node temp = head;

      while(temp!=null)
      {
        System.out.print("  "+temp.data);

        temp=temp.next;
      }
    }
  }


  
  public static void main(String[] args) 
  {
    LinkedList obj = new LinkedList();
    //insert following linked list nodes
    obj.insert(80);
    obj.insert(70);
    obj.insert(60);
    obj.insert(50);
    obj.insert(40);
    obj.insert(30);
    obj.insert(20);
    obj.insert(10);
    obj.display();  
  }
}

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
Insert node in front of doubly linked list node
//C# Program 
//insert new node at beginning of doubly linked list

//Define class of linked list Node
using System;
public class Node {
  public int data;
  public Node next;
  public Node prev;
  public Node(int data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}
public class LinkedList {
  public Node head;
  //Class constructors
  LinkedList() {
    head = null;
  }
  //insert node at beginning of linke list
  public void insert(int value) {
    //Create a node
    Node node = new Node(value);
    node.next = head;
    //when linked list is not empty

    if (head != null) {
      head.prev = node;
    }
    head = node;
  }
  //Display node element of doubly linked list
  public void display() {
    if (head == null) {
      Console.WriteLine("Empty Linked List");
    } else {
      Console.Write("Doubly Linked List Element :");
      Node temp = head;
      while (temp != null) {
        Console.Write(" " + temp.data);
        temp = temp.next;
      }
    }
  }
  public static void Main(String[] args) {
    LinkedList obj = new LinkedList();
    obj.insert(80);
    obj.insert(70);
    obj.insert(60);
    obj.insert(50);
    obj.insert(40);
    obj.insert(30);
    obj.insert(20);
    obj.insert(10);
    obj.display();
  }
}

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
<?php
//Php Program 
//insert new node at beginning of doubly linked list
//Define class of linked list Node
class Node {
  public $data;
  public $next;
  public $prev;

  function __construct($data) {
    $this->data = $data;
    $this->next = null;
    $this->prev = null;
  }
}
class LinkedList {
  public $head;
  //Class constructors

  function __construct() {
    $this->head = null;
  }
  //insert node at beginning of linke list

  public  function insert($value) {
    //Create a node
    $node = new Node($value);
    $node->next = $this->head;
    //when linked list is not empty

    if ($this->head != null) {
      $this->head->prev = $node;
    }
    $this->head = $node;
  }
  //Display node element of doubly linked list

  public  function display() {
    if ($this->head == null) {
      echo("Empty Linked List");
    } else {
      echo("Doubly Linked List Element :");
      $temp = $this->head;
      while ($temp != null) {
        echo(" ". $temp->data);
        $temp = $temp->next;
      }
    }
  }
}

function main() {
  $obj = new LinkedList();
  //insert following linked list nodes
  $obj->insert(80);
  $obj->insert(70);
  $obj->insert(60);
  $obj->insert(50);
  $obj->insert(40);
  $obj->insert(30);
  $obj->insert(20);
  $obj->insert(10);
  $obj->display();

}
main();

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
//Node Js Program 
//insert new node at beginning of doubly linked list

//Define class of linked list Node
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}
class LinkedList {
  //Class constructors

  constructor() {
    this.head = null;
  }

  //insert node at beginning of linke list
  insert(value) {
    //Create a node
    var node = new Node(value);
    node.next = this.head;
    //when linked list is not empty

    if (this.head != null) {
      this.head.prev = node;
    }
    this.head = node;
  }

  //Display node element of doubly linked list
  display() {
    if (this.head == null) {
      process.stdout.write("Empty Linked List");
    } else {
      process.stdout.write("Doubly Linked List Element :");
      var temp = this.head;
      while (temp != null) {
        process.stdout.write(" " + temp.data);
        temp = temp.next;
      }
    }
  }
}

function main(args) {
  var obj = new LinkedList();
  //insert following linked list nodes
  obj.insert(80);
  obj.insert(70);
  obj.insert(60);
  obj.insert(50);
  obj.insert(40);
  obj.insert(30);
  obj.insert(20);
  obj.insert(10);
  obj.display();
}

main();

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
# Python 3 Program 
# insert new node at beginning of doubly linked list

# Define class of linked list Node
class Node :
  
  def __init__(self, data) :
    self.data = data
    self.next = None
    self.prev = None
  

class LinkedList :
  
  # Class constructors
  def __init__(self) :
    self.head = None
  
  # insert node at beginning of linke list
  def insert(self, value) :
    # Create a node
    node = Node(value)
    node.next = self.head
    # when linked list is not empty

    if (self.head != None) :
      self.head.prev = node
    
    self.head = node
  
  # Display node element of doubly linked list
  def display(self) :
    if (self.head == None) :
      print("Empty Linked List", end = "")
    else :
      print("Doubly Linked List Element :", end = "")
      temp = self.head
      while (temp != None) :
        print(" ", temp.data, end = "")
        temp = temp.next
      
    
  

def main() :
  obj = LinkedList()
  # insert following linked list nodes
  obj.insert(80)
  obj.insert(70)
  obj.insert(60)
  obj.insert(50)
  obj.insert(40)
  obj.insert(30)
  obj.insert(20)
  obj.insert(10)
  obj.display()


if __name__ == "__main__":
  main()

Output

Doubly Linked List Element :  10  20  30  40  50  60  70  80
# Ruby Program 
# insert new node at beginning of doubly linked list

# Define class of linked list Node
class Node
    # Define the accessor and reader of class Node
    attr_reader :data, :next_node, :prev
    attr_accessor :data, :next_node, :prev 
  
  def initialize(data) 
    self.data = data
    self.next_node = nil
    self.prev = nil
  end
end
class LinkedList
    # Define the accessor and reader of class LinkedList
    attr_reader :head
    attr_accessor :head 
  
  # Class constructors
  def initialize() 
    @head = nil
  end
  # insert node at beginning of linke list
  def insert(value) 
    # Create a node
    node = Node.new(value)
    node.next_node = @head
    # when linked list is not empty

    if (@head != nil) 
      @head.prev = node
    end
    @head = node
  end
  # Display node element of doubly linked list
  def display() 
    if (@head == nil) 
      print("Empty Linked List")
    else 
      print("Doubly Linked List Element  :")
      temp = @head
      while (temp != nil) 
        print(" ", temp.data)
        temp = temp.next_node
      end
    end
  end
end
def main() 
  obj = LinkedList.new()
  # insert following linked list nodes
  obj.insert(80)
  obj.insert(70)
  obj.insert(60)
  obj.insert(50)
  obj.insert(40)
  obj.insert(30)
  obj.insert(20)
  obj.insert(10)
  obj.display()
end
main()

Output

Doubly Linked List Element  : 10 20 30 40 50 60 70 80
//Scala Program 
//insert new node at beginning of doubly linked list
//Define class of linked list Node
class Node(var data: Int,
  var next: Node,
    var prev: Node) {
  def this(data: Int) {
    this( data, null, null);
  }
}
class LinkedList(var head: Node) {

  //Class constructors
  def this() {
    this(null);
  }
  //insert node at beginning of linke list
  def insert(value: Int): Unit = {
    //Create a node
    var node: Node = new Node(value);
    node.next = head;

    //when linked list is not empty

    if (head != null) {
      head.prev = node;
    }
    head = node;
  }
  //Display node element of doubly linked list
  def display(): Unit = {
    if (head == null) {
      print("Empty Linked List");
    } else {
      print("Doubly Linked List Element :");
      var temp: Node = head;
      while (temp != null) {
        print(" " + temp.data);
        temp = temp.next;
      }
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: LinkedList = new LinkedList();
    //insert following linked list nodes
    obj.insert(80);
    obj.insert(70);
    obj.insert(60);
    obj.insert(50);
    obj.insert(40);
    obj.insert(30);
    obj.insert(20);
    obj.insert(10);
    obj.display();
  }
}

Output

Doubly Linked List Element : 10 20 30 40 50 60 70 80
//Swift Program 
//insert new node at beginning of doubly linked list
//Define class of linked list Node
class Node {
  var data: Int;
  var next: Node? ;
  var prev: Node? ;
  init(_ data: Int) {
    self.data = data;
    self.next = nil;
    self.prev = nil;
  }
}
class LinkedList {
  var head: Node? ;
  //Class constructors

  init() {
    self.head = nil;
  }
  //insert node at beginning of linke list
  func insert(_ value: Int) {
    //Create a node
    let node: Node? = Node(value);
    node!.next = self.head;
    //when linked list is not empty

    if (self.head != nil) {
      self.head!.prev = node;
    }
    self.head = node;
  }
  //Display node element of doubly linked list
  func display() {
    if (self.head == nil) {
      print("Empty Linked List", terminator: "");
    } else {
      print("Doubly Linked List Element :", terminator: "");
      var temp: Node? = self.head;
      while (temp != nil) {
        print(" ", temp!.data, terminator: "");
        temp = temp!.next;
      }
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  //insert following linked list nodes
  obj.insert(80);
  obj.insert(70);
  obj.insert(60);
  obj.insert(50);
  obj.insert(40);
  obj.insert(30);
  obj.insert(20);
  obj.insert(10);
  obj.display();
}
main();

Output

Doubly Linked List Element :  10  20  30  40  50  60  70  80

In this above example doubly linked list are accessed by first node. Observed that in case we are have one more variable which are point to last node then we can access both direction of linked list.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved