Posted on by Kalkicode

# Insert node at end of doubly linked list

There are two possible way to insert doubly linked list at the end of linked list. First only use one pointer. And second is using two pointers.

First approach: This is very simplest method when linked list empty then insert first node of linked list and pointers value are assigned to NULL. When linked list are not empty then find last node by using traversal of linked list.

But note that when we add new node to last position. new created node are have two pointer fields (next and prev points). next pointer are assigned to NULL and pervious pointer are assign to the reference of previous nodes.

In this process are need only one pointer to hold the reference of first linked list nodes. The time complexity of this process is O(n) to find last node of linked list. Suppose we are inserted the following (1,2,3,4,5,6,7,8) node in a sequence.

Here given code implementation process.

``````// Java Program
// insert new node at end of doubly linked list
// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
{
// Set inital value
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
{
return;
}
// Find last node
while (temp.next != null)
{
// Visit to next node
temp = temp.next;
}
// Add node at the end position
temp.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
public void display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
System.out.print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
public static void main(String[] args)
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````// C Program
// insert new node at end of doubly linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

//Create structure
{
int data;
};
{
};
{
return list;
}
// Insert Node at the end pf linked list
void insert(struct DoublyLinkedList *dll, int value)
{
// Create a dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->next = NULL;
node->data = value;
{
node->prev = NULL;
// When empty list
return;
}
// Find last node
while (temp->next != NULL)
{
// Visit to next node
temp = temp->next;
}
// Add node at the end position
temp->next = node;
node->prev = temp;
}
}
//Display element of Node
{
{
}
else
{
//Traverse doubly linked list from front to rear
while (temp != NULL)
{
//print node value
printf("%d  ", temp->data);
temp = temp->next;
}
}
}
int main()
{
insert(dll, 1);
insert(dll, 2);
insert(dll, 3);
insert(dll, 4);
insert(dll, 5);
insert(dll, 6);
insert(dll, 7);
insert(dll, 8);
//display all node
display(dll);
return 0;
}``````

#### Output

``Linked List Elements : 1  2  3  4  5  6  7  8``
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
public: int data;
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
{
{
}
// Insert new node at end position
void insert(int value)
{
// Create a node
{
return;
}
// Find last node
while (temp->next != NULL)
{
// Visit to next node
temp = temp->next;
}
// Add node at the end position
temp->next = node;
node->prev = temp;
}
// Display node element of doubly linked list
void display()
{
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Doubly Linked List Element :";
// Get first node of linked list
while (temp != NULL)
{
// Display node value
cout << "  " << temp->data;
// Visit to next node
temp = temp->next;
}
}
}
};
int main()
{
// Insert following linked list nodes
dll->insert(1);
dll->insert(2);
dll->insert(3);
dll->insert(4);
dll->insert(5);
dll->insert(6);
dll->insert(7);
dll->insert(8);
dll->display();
return 0;
}``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````package main
import "fmt"
// Go Program
// insert new node at end of doubly linked list

// Define class of linked list Node
data int
}
me.data = data
me.next = nil
me.prev = nil
return me
}
}
// Set inital value
return me
}
// Insert new node at end position
// Create a node
return
}
// Find last node
for (temp.next != nil) {
// Visit to next node
temp = temp.next
}
// Add node at the end position
temp.next = node
node.prev = temp
}
// Display node element of doubly linked list
} else {
// Get first node of linked list
for (temp != nil) {
// Display node value
fmt.Print("  ", temp.data)
// Visit to next node
temp = temp.next
}
}
}
func main() {
// Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()
}``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````// Include namespace system
using System;
// Csharp Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
{
// Set inital value
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
{
return;
}
// Find last node
while (temp.next != null)
{
// Visit to next node
temp = temp.next;
}
// Add node at the end position
temp.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
public void display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
Console.Write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
public static void Main(String[] args)
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````<?php
// Php Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
public \$data;
public \$next;
public \$prev;
public  function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
\$this->prev = NULL;
}
}
{
public  function __construct()
{
}
// Insert new node at end position
public  function insert(\$value)
{
// Create a node
{
return;
}
// Find last node
while (\$temp->next != NULL)
{
// Visit to next node
\$temp = \$temp->next;
}
// Add node at the end position
\$temp->next = \$node;
\$node->prev = \$temp;
}
// Display node element of doubly linked list
public  function display()
{
{
"\n");
}
else
{
// Get first node of linked list
while (\$temp != NULL)
{
// Display node value
print_r("  ".strval(\$temp->data));
// Visit to next node
\$temp = \$temp->next;
}
}
}
public static
function main(\$args)
{
// Insert following linked list nodes
\$dll->insert(1);
\$dll->insert(2);
\$dll->insert(3);
\$dll->insert(4);
\$dll->insert(5);
\$dll->insert(6);
\$dll->insert(7);
\$dll->insert(8);
\$dll->display();
}
}

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````// Node JS Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
constructor()
{
}
// Insert new node at end position
insert(value)
{
// Create a node
{
return;
}
// Find last node
while (temp.next != null)
{
// Visit to next node
temp = temp.next;
}
// Add node at the end position
temp.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
process.stdout.write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
}

function main()
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
main();``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````#  Python 3 Program
#  insert new node at end of doubly linked list

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

def __init__(self) :

#  Insert new node at end position
def insert(self, value) :
#  Create a node
return

#  Find last node
while (temp.next != None) :
#  Visit to next node
temp = temp.next

#  Add node at the end position
temp.next = node
node.prev = temp

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

def main() :
#  Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()

if __name__ == "__main__": main()``````

#### Output

``Doubly Linked List Element :   1   2   3   4   5   6   7   8``
``````#  Ruby Program
#  insert new node at end of doubly linked list

#  Define class of linked list Node
attr_accessor :data, :next, :prev
def initialize(data)
self.data = data
self.next = nil
self.prev = nil
end

end

def initialize()
end

#  Insert new node at end position
def insert(value)
#  Create a node
return
end

#  Find last node
while (temp.next != nil)
#  Visit to next node
temp = temp.next
end

#  Add node at the end position
temp.next = node
node.prev = temp
end

#  Display node element of doubly linked list
def display()
else

#  Get first node of linked list
while (temp != nil)
#  Display node value
print("  ", temp.data)
#  Visit to next node
temp = temp.next
end

end

end

end

def main()
#  Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()
end

main()``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````// Scala Program
// insert new node at end of doubly linked list
// Define class of linked list Node
{
def this(data: Int)
{
this(data, null, null);
}
}
{
def this()
{
this(null);
}
// Insert new node at end position
def insert(value: Int): Unit = {
// Create a node
{
return;
}
// Find last node
while (temp.next != null)
{
// Visit to next node
temp = temp.next;
}
// Add node at the end position
temp.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
def display(): Unit = {
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``
``````// Swift 4 Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
self.prev = nil;
}
}
{
init()
{
}
// Insert new node at end position
func insert(_ value: Int)
{
// Create a node
{
return;
}
// Find last node
while (temp!.next  != nil)
{
// Visit to next node
temp = temp!.next;
}
// Add node at the end position
temp!.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
func display()
{
{
}
else
{
print("Doubly Linked List Element :", terminator: "");
// Get first node of linked list
while (temp  != nil)
{
// Display node value
print("  ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
}
}
}
func main()
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
main();``````

#### Output

``Doubly Linked List Element :   1   2   3   4   5   6   7   8``
``````// Kotlin Program
// insert new node at end of doubly linked list

// Define class of linked list Node
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
constructor()
{
}
// Insert new node at end position
fun insert(value: Int): Unit
{
// Create a node
{
return;
}
// Find last node
while (temp?.next != null)
{
// Visit to next node
temp = temp.next;
}
// Add node at the end position
temp?.next = node;
node.prev = temp;
}
// Display node element of doubly linked list
fun display(): Unit
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}``````

#### Output

``Doubly Linked List Element :  1  2  3  4  5  6  7  8``

Second approach: In this process are used to pointers head and tail, head pointer are hold the reference of first node of linked list and second tail pointer are holding the reference of last node.

When we are add new node to this doubly linked list so there is no need to find last node, Because tail pointer are already hold the reference of last node . Add this new node to last position by using of tail pointer. And assign new node address to tail pointer.

``````// Java Program
// insert new node at end of doubly linked list set B

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
{
// Set inital value
this.tail = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
{
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
public void display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
System.out.print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
// Get last node of linked list
temp = this.tail;
while (temp != null)
{
// Display node value
System.out.print("  " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
public static void main(String[] args)
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````// C Program
// insert new node at end of doubly linked list set B
#include <stdio.h>
//For malloc function
#include <stdlib.h>

// Create structure
{
int data;
};
{
};
{
list->tail = NULL;
return list;
}
// Insert Node at the end pf linked list
void insert(struct DoublyLinkedList *dll, int value)
{
// Create a dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->next = NULL;
node->prev = NULL;
node->data = value;
{
dll->tail = node;
return;
}
// Add node at last position
dll->tail->next = node;
node->prev = dll->tail;
dll->tail = node;
}
}
// Display element of Node
{
{
}
else
{
//Traverse doubly linked list from front to rear
while (temp != NULL)
{
//print node value
printf("%d  ", temp->data);
temp = temp->next;
}
temp = dll->tail;
//Traverse doubly linked list from front to rear
while (temp != NULL)
{
//print node value
printf("%d  ", temp->data);
temp = temp->prev;
}
}
}
int main()
{
insert(dll, 1);
insert(dll, 2);
insert(dll, 3);
insert(dll, 4);
insert(dll, 5);
insert(dll, 6);
insert(dll, 7);
insert(dll, 8);
//display all node
display(dll);
return 0;
}``````

#### Output

`````` Linked List Head to Tail : 1  2  3  4  5  6  7  8
Linked List Tail to Head : 8  7  6  5  4  3  2  1``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// insert new node at end of doubly linked list set B

// Define class of linked list Node
{
public: int data;
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
{
{
this->tail = NULL;
}
// Insert new node at end position
void insert(int value)
{
// Create a node
{
this->tail = node;
return;
}
// Add node at last position
this->tail->next = node;
node->prev = this->tail;
this->tail = node;
}
// Display node element of doubly linked list
void display()
{
{
cout << "Empty Linked List" << endl;
}
else
{
// Get first node of linked list
while (temp != NULL)
{
// Display node value
cout << "  " << temp->data;
// Visit to next node
temp = temp->next;
}
// Get last node of linked list
temp = this->tail;
while (temp != NULL)
{
// Display node value
cout << "  " << temp->data;
// Visit to prev node
temp = temp->prev;
}
}
}
};
int main()
{
// Insert following linked list nodes
dll->insert(1);
dll->insert(2);
dll->insert(3);
dll->insert(4);
dll->insert(5);
dll->insert(6);
dll->insert(7);
dll->insert(8);
dll->display();
return 0;
}``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````package main
import "fmt"
// Go Program
// insert new node at end of doubly linked list set B
// Define class of linked list Node
data int
}
me.data = data
me.next = nil
me.prev = nil
return me
}
}
// Set inital value
me.tail = nil
return me
}
// Insert new node at end position
// Create a node
this.tail = node
return
}
// Add node at last position
this.tail.next = node
node.prev = this.tail
this.tail = node
}
// Display node element of doubly linked list
} else {
// Get first node of linked list
for (temp != nil) {
// Display node value
fmt.Print("  ", temp.data)
// Visit to next node
temp = temp.next
}
// Get last node of linked list
temp = this.tail
for (temp != nil) {
// Display node value
fmt.Print("  ", temp.data)
// Visit to prev node
temp = temp.prev
}
}
}
func main() {
// Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()
}``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````// Include namespace system
using System;
// Csharp Program
// insert new node at end of doubly linked list set B

// Define class of linked list Node
{
public int data;
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
{
// Set inital value
this.tail = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
{
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
public void display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
Console.Write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
// Get last node of linked list
temp = this.tail;
while (temp != null)
{
// Display node value
Console.Write("  " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
public static void Main(String[] args)
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````<?php
// Php Program
// insert new node at end of doubly linked list set B
// Define class of linked list Node
{
public \$data;
public \$next;
public \$prev;
public  function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
\$this->prev = NULL;
}
}
{
public \$tail;
public  function __construct()
{
\$this->tail = NULL;
}
// Insert new node at end position
public  function insert(\$value)
{
// Create a node
{
\$this->tail = \$node;
return;
}
// Add node at last position
\$this->tail->next = \$node;
\$node->prev = \$this->tail;
\$this->tail = \$node;
}
// Display node element of doubly linked list
public  function display()
{
{
"\n");
}
else
{
// Get first node of linked list
while (\$temp != NULL)
{
// Display node value
print_r("  ".strval(\$temp->data));
// Visit to next node
\$temp = \$temp->next;
}
// Get last node of linked list
\$temp = \$this->tail;
while (\$temp != NULL)
{
// Display node value
print_r("  ".strval(\$temp->data));
// Visit to prev node
\$temp = \$temp->prev;
}
}
}
public static
function main(\$args)
{
// Insert following linked list nodes
\$dll->insert(1);
\$dll->insert(2);
\$dll->insert(3);
\$dll->insert(4);
\$dll->insert(5);
\$dll->insert(6);
\$dll->insert(7);
\$dll->insert(8);
\$dll->display();
}
}

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````// Node JS Program
// insert new node at end of doubly linked list set B

// Define class of linked list Node
{
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
constructor()
{
this.tail = null;
}
// Insert new node at end position
insert(value)
{
// Create a node
{
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
display()
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
process.stdout.write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
// Get last node of linked list
temp = this.tail;
while (temp != null)
{
// Display node value
process.stdout.write("  " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}

function main()
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
main();``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````#  Python 3 Program
#  insert new node at end of doubly linked list set B

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

def __init__(self) :
self.tail = None

#  Insert new node at end position
def insert(self, value) :
#  Create a node
self.tail = node
return

#  Add node at last position
self.tail.next = node
node.prev = self.tail
self.tail = node

#  Display node element of doubly linked list
def display(self) :
else :
#  Get first node of linked list
while (temp != None) :
#  Display node value
print("  ", temp.data, end = "")
#  Visit to next node
temp = temp.next

#  Get last node of linked list
temp = self.tail
while (temp != None) :
#  Display node value
print("  ", temp.data, end = "")
#  Visit to prev node
temp = temp.prev

def main() :
#  Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()

if __name__ == "__main__": main()``````

#### Output

``````Linked List Head to Tail :   1   2   3   4   5   6   7   8
Linked List Tail to Head :   8   7   6   5   4   3   2   1``````
``````#  Ruby Program
#  insert new node at end of doubly linked list set B

#  Define class of linked list Node
attr_accessor :data, :next, :prev
def initialize(data)
self.data = data
self.next = nil
self.prev = nil
end

end

def initialize()
self.tail = nil
end

#  Insert new node at end position
def insert(value)
#  Create a node
self.tail = node
return
end

#  Add node at last position
self.tail.next = node
node.prev = self.tail
self.tail = node
end

#  Display node element of doubly linked list
def display()
else

#  Get first node of linked list
while (temp != nil)
#  Display node value
print("  ", temp.data)
#  Visit to next node
temp = temp.next
end

#  Get last node of linked list
temp = self.tail
while (temp != nil)
#  Display node value
print("  ", temp.data)
#  Visit to prev node
temp = temp.prev
end

end

end

end

def main()
#  Insert following linked list nodes
dll.insert(1)
dll.insert(2)
dll.insert(3)
dll.insert(4)
dll.insert(5)
dll.insert(6)
dll.insert(7)
dll.insert(8)
dll.display()
end

main()``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````// Scala Program
// insert new node at end of doubly linked list set B

// Define class of linked list Node
{
def this(data: Int)
{
this(data, null, null);
}
}
{
def this()
{

this(null, null);
}
// Insert new node at end position
def insert(value: Int): Unit = {
// Create a node
{
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
def display(): Unit = {
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
// Get last node of linked list
temp = this.tail;
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
}``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````
``````// Swift 4 Program
// insert new node at end of doubly linked list set B
// Define class of linked list Node
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
self.prev = nil;
}
}
{
init()
{
self.tail = nil;
}
// Insert new node at end position
func insert(_ value: Int)
{
// Create a node
{
self.tail = node;
return;
}
// Add node at last position
self.tail!.next = node;
node.prev = self.tail;
self.tail = node;
}
// Display node element of doubly linked list
func display()
{
{
}
else
{
// Get first node of linked list
while (temp  != nil)
{
// Display node value
print("  ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
// Get last node of linked list
temp = self.tail;
while (temp  != nil)
{
// Display node value
print("  ", temp!.data, terminator: "");
// Visit to prev node
temp = temp!.prev;
}
}
}
}
func main()
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}
main();``````

#### Output

``````Linked List Head to Tail :   1   2   3   4   5   6   7   8
Linked List Tail to Head :   8   7   6   5   4   3   2   1``````
``````// Kotlin Program
// insert new node at end of doubly linked list set B
// Define class of linked list Node
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
{
constructor()
{
this.tail = null;
}
// Insert new node at end position
fun insert(value: Int): Unit
{
// Create a node
{
this.tail = node;
return;
}
// Add node at last position
this.tail?.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
fun display(): Unit
{
{
}
else
{
// Get first node of linked list
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
// Get last node of linked list
temp = this.tail;
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.display();
}``````

#### Output

``````Linked List Head to Tail :  1  2  3  4  5  6  7  8
Linked List Tail to Head :  8  7  6  5  4  3  2  1``````

## Comment

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.

Categories
Relative Post