# 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.

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;
};

//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->prev=NULL;
{
//When non empty list
}
}
}
//Display element of Node
void display()
{

{
}
else
{
//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
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;
}
};
public:
//Class constructors

}
//insert node at beginning of linke list
void insert(int value) {
//Create a node
Node *node = new Node(value);
//when linked list is not empty

}
}
//Display node element of doubly linked list
void display() {
} else {
cout << "Doubly Linked List Element :";
while (temp != NULL) {
cout << " " << temp->data;
temp = temp->next;
}
}
}
};
int main() {
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;
}
}
{

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

//when linked list is not empty
{
}

}
//Display node element of doubly linked list
public void display()
{
{
}
else
{

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

temp=temp.next;
}
}
}

public static void main(String[] args)
{
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``
``````//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;
}
}
//Class constructors
}
//insert node at beginning of linke list
public void insert(int value) {
//Create a node
Node node = new Node(value);
//when linked list is not empty

}
}
//Display node element of doubly linked list
public void display() {
} else {
while (temp != null) {
Console.Write(" " + temp.data);
temp = temp.next;
}
}
}
public static void Main(String[] args) {
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 constructors

function __construct() {
}
//insert node at beginning of linke list

public  function insert(\$value) {
//Create a node
\$node = new Node(\$value);
//when linked list is not empty

}
}
//Display node element of doubly linked list

public  function display() {
} else {
while (\$temp != null) {
echo(" ". \$temp->data);
\$temp = \$temp->next;
}
}
}
}

function main() {
\$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 constructors

constructor() {
}

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

}
}

//Display node element of doubly linked list
display() {
} else {
while (temp != null) {
process.stdout.write(" " + temp.data);
temp = temp.next;
}
}
}
}

function main(args) {
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 constructors
def __init__(self) :

# insert node at beginning of linke list
def insert(self, value) :
# Create a node
node = Node(value)
# when linked list is not empty

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

def main() :
# 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_accessor :data, :next_node, :prev

def initialize(data)
self.data = data
self.next_node = nil
self.prev = nil
end
end

# Class constructors
def initialize()
end
# insert node at beginning of linke list
def insert(value)
# Create a node
node = Node.new(value)
# when linked list is not empty

end
end
# Display node element of doubly linked list
def display()
else
while (temp != nil)
print(" ", temp.data)
temp = temp.next_node
end
end
end
end
def main()
# 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 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);

//when linked list is not empty

}
}
//Display node element of doubly linked list
def display(): Unit = {
} else {
while (temp != null) {
print(" " + temp.data);
temp = temp.next;
}
}
}
}
object Main {
def main(args: Array[String]): Unit = {
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 constructors

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

}
}
//Display node element of doubly linked list
func display() {
} else {
print("Doubly Linked List Element :", terminator: "");
while (temp != nil) {
print(" ", temp!.data, terminator: "");
temp = temp!.next;
}
}
}
}
func main() {
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.