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
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
public class DoublyLinkedList
{
public LinkNode head;
public DoublyLinkedList()
{
// Set inital value
this.head = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
return;
}
LinkNode temp = this.head;
// 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()
{
if (this.head == null)
{
System.out.println("Empty Linked List");
}
else
{
System.out.print("Doubly Linked List Element :");
// Get first node of linked list
LinkNode temp = this.head;
// iterate 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)
{
DoublyLinkedList dll = new DoublyLinkedList();
// 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
struct LinkNode
{
int data;
struct LinkNode *next;
struct LinkNode *prev;
};
struct DoublyLinkedList
{
struct LinkNode *head;
};
struct DoublyLinkedList *newDLL()
{
struct DoublyLinkedList *list = (struct DoublyLinkedList *)
malloc(sizeof(struct DoublyLinkedList));
list->head = NULL;
return list;
}
// Insert Node at the end pf linked list
void insert(struct DoublyLinkedList *dll, int value)
{
// Create a dynamic node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->next = NULL;
node->data = value;
if (dll->head == NULL)
{
node->prev = NULL;
// When empty list
dll->head = node;
return;
}
struct LinkNode *temp = dll->head;
// 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
void display(struct DoublyLinkedList *dll)
{
if (dll->head == NULL)
{
printf("Empty linked list");
}
else
{
printf("\nLinked List Elements : ");
struct LinkNode *temp = dll->head;
//Traverse doubly linked list from front to rear
while (temp != NULL)
{
//print node value
printf("%d ", temp->data);
temp = temp->next;
}
}
}
int main()
{
struct DoublyLinkedList *dll = newDLL();
//Insert element of linked list
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
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode *prev;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
class DoublyLinkedList
{
public: LinkNode *head;
DoublyLinkedList()
{
this->head = NULL;
}
// Insert new node at end position
void insert(int value)
{
// Create a node
LinkNode *node = new LinkNode(value);
if (this->head == NULL)
{
// Add first node
this->head = node;
return;
}
LinkNode *temp = this->head;
// 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()
{
if (this->head == NULL)
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Doubly Linked List Element :";
// Get first node of linked list
LinkNode *temp = this->head;
// iterate linked list
while (temp != NULL)
{
// Display node value
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
}
}
}
};
int main()
{
DoublyLinkedList *dll = new DoublyLinkedList();
// 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
type LinkNode struct {
data int
next * LinkNode
prev * LinkNode
}
func getLinkNode(data int) * LinkNode {
var me *LinkNode = &LinkNode {}
me.data = data
me.next = nil
me.prev = nil
return me
}
type DoublyLinkedList struct {
head * LinkNode
}
func getDoublyLinkedList() * DoublyLinkedList {
var me *DoublyLinkedList = &DoublyLinkedList {}
// Set inital value
me.head = nil
return me
}
// Insert new node at end position
func(this *DoublyLinkedList) insert(value int) {
// Create a node
var node * LinkNode = getLinkNode(value)
if this.head == nil {
// Add first node
this.head = node
return
}
var temp * LinkNode = this.head
// 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
func(this DoublyLinkedList) display() {
if this.head == nil {
fmt.Println("Empty Linked List")
} else {
fmt.Print("Doubly Linked List Element :")
// Get first node of linked list
var temp * LinkNode = this.head
// iterate linked list
for (temp != nil) {
// Display node value
fmt.Print(" ", temp.data)
// Visit to next node
temp = temp.next
}
}
}
func main() {
var dll * DoublyLinkedList = getDoublyLinkedList()
// 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 class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
public class DoublyLinkedList
{
public LinkNode head;
public DoublyLinkedList()
{
// Set inital value
this.head = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
return;
}
LinkNode temp = this.head;
// 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()
{
if (this.head == null)
{
Console.WriteLine("Empty Linked List");
}
else
{
Console.Write("Doubly Linked List Element :");
// Get first node of linked list
LinkNode temp = this.head;
// iterate 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)
{
DoublyLinkedList dll = new DoublyLinkedList();
// 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
class LinkNode
{
public $data;
public $next;
public $prev;
public function __construct($data)
{
$this->data = $data;
$this->next = NULL;
$this->prev = NULL;
}
}
class DoublyLinkedList
{
public $head;
public function __construct()
{
$this->head = NULL;
}
// Insert new node at end position
public function insert($value)
{
// Create a node
$node = new LinkNode($value);
if ($this->head == NULL)
{
// Add first node
$this->head = $node;
return;
}
$temp = $this->head;
// 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()
{
if ($this->head == NULL)
{
print_r("Empty Linked List".
"\n");
}
else
{
print_r("Doubly Linked List Element :");
// Get first node of linked list
$temp = $this->head;
// iterate 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)
{
$dll = new DoublyLinkedList();
// 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();
}
}
DoublyLinkedList::main(array());
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
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList
{
constructor()
{
this.head = null;
}
// Insert new node at end position
insert(value)
{
// Create a node
var node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
return;
}
var temp = this.head;
// 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()
{
if (this.head == null)
{
console.log("Empty Linked List");
}
else
{
process.stdout.write("Doubly Linked List Element :");
// Get first node of linked list
var temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
}
function main()
{
var dll = new DoublyLinkedList();
// 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
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList :
def __init__(self) :
self.head = None
# Insert new node at end position
def insert(self, value) :
# Create a node
node = LinkNode(value)
if (self.head == None) :
# Add first node
self.head = node
return
temp = self.head
# 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) :
if (self.head == None) :
print("Empty Linked List")
else :
print("Doubly Linked List Element :", end = "")
# Get first node of linked list
temp = self.head
# iterate linked list
while (temp != None) :
# Display node value
print(" ", temp.data, end = "")
# Visit to next node
temp = temp.next
def main() :
dll = DoublyLinkedList()
# 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
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next, :prev
attr_accessor :data, :next, :prev
def initialize(data)
self.data = data
self.next = nil
self.prev = nil
end
end
class DoublyLinkedList
# Define the accessor and reader of class DoublyLinkedList
attr_reader :head
attr_accessor :head
def initialize()
self.head = nil
end
# Insert new node at end position
def insert(value)
# Create a node
node = LinkNode.new(value)
if (self.head == nil)
# Add first node
self.head = node
return
end
temp = self.head
# 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()
if (self.head == nil)
print("Empty Linked List", "\n")
else
print("Doubly Linked List Element :")
# Get first node of linked list
temp = self.head
# iterate linked list
while (temp != nil)
# Display node value
print(" ", temp.data)
# Visit to next node
temp = temp.next
end
end
end
end
def main()
dll = DoublyLinkedList.new()
# 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
class LinkNode(var data: Int,
var next: LinkNode,
var prev: LinkNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
class DoublyLinkedList(var head: LinkNode)
{
def this()
{
this(null);
}
// Insert new node at end position
def insert(value: Int): Unit = {
// Create a node
var node: LinkNode = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
return;
}
var temp: LinkNode = this.head;
// 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 = {
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Doubly Linked List Element :");
// Get first node of linked list
var temp: LinkNode = this.head;
// iterate 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 = {
var dll: DoublyLinkedList = new DoublyLinkedList();
// 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
class LinkNode
{
var data: Int;
var next: LinkNode? ;
var prev: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
self.prev = nil;
}
}
class DoublyLinkedList
{
var head: LinkNode? ;
init()
{
self.head = nil;
}
// Insert new node at end position
func insert(_ value: Int)
{
// Create a node
let node: LinkNode = LinkNode(value);
if (self.head == nil)
{
// Add first node
self.head = node;
return;
}
var temp: LinkNode? = self.head;
// 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()
{
if (self.head == nil)
{
print("Empty Linked List");
}
else
{
print("Doubly Linked List Element :", terminator: "");
// Get first node of linked list
var temp: LinkNode? = self.head;
// iterate linked list
while (temp != nil)
{
// Display node value
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
}
}
}
func main()
{
let dll: DoublyLinkedList = DoublyLinkedList();
// 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
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
var prev: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList
{
var head: LinkNode ? ;
constructor()
{
this.head = null;
}
// Insert new node at end position
fun insert(value: Int): Unit
{
// Create a node
val node: LinkNode = LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
return;
}
var temp: LinkNode ? = this.head;
// 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
{
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Doubly Linked List Element :");
// Get first node of linked list
var temp: LinkNode ? = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
}
}
}
fun main(args: Array < String > ): Unit
{
val dll: DoublyLinkedList = DoublyLinkedList();
// 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
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
public class DoublyLinkedList
{
public LinkNode head;
public LinkNode tail;
public DoublyLinkedList()
{
// Set inital value
this.head = null;
this.tail = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = 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()
{
if (this.head == null)
{
System.out.println("Empty Linked List");
}
else
{
System.out.print("Linked List Head to Tail :");
// Get first node of linked list
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
System.out.print("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
System.out.print(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
public static void main(String[] args)
{
DoublyLinkedList dll = new DoublyLinkedList();
// 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
struct LinkNode
{
int data;
struct LinkNode *next;
struct LinkNode *prev;
};
struct DoublyLinkedList
{
struct LinkNode *head;
struct LinkNode *tail;
};
struct DoublyLinkedList *newDLL()
{
struct DoublyLinkedList *list = (struct DoublyLinkedList *)
malloc(sizeof(struct DoublyLinkedList));
list->head = NULL;
list->tail = NULL;
return list;
}
// Insert Node at the end pf linked list
void insert(struct DoublyLinkedList *dll, int value)
{
// Create a dynamic node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
node->next = NULL;
node->prev = NULL;
node->data = value;
if (dll->head == NULL)
{
// Add first node
dll->head = node;
dll->tail = node;
return;
}
// Add node at last position
dll->tail->next = node;
node->prev = dll->tail;
dll->tail = node;
}
}
// Display element of Node
void display(struct DoublyLinkedList *dll)
{
if (dll->head == NULL)
{
printf("Empty linked list");
}
else
{
printf("\n Linked List Head to Tail : ");
struct LinkNode *temp = dll->head;
//Traverse doubly linked list from front to rear
while (temp != NULL)
{
//print node value
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n Linked List Tail to Head : ");
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()
{
struct DoublyLinkedList *dll = newDLL();
//Insert element of linked list
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
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode *prev;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
class DoublyLinkedList
{
public: LinkNode *head;
LinkNode *tail;
DoublyLinkedList()
{
this->head = NULL;
this->tail = NULL;
}
// Insert new node at end position
void insert(int value)
{
// Create a node
LinkNode *node = new LinkNode(value);
if (this->head == NULL)
{
// Add first node
this->head = 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()
{
if (this->head == NULL)
{
cout << "Empty Linked List" << endl;
}
else
{
cout << "Linked List Head to Tail :";
// Get first node of linked list
LinkNode *temp = this->head;
// iterate linked list
while (temp != NULL)
{
// Display node value
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
}
cout << "\nLinked List Tail to Head :";
// Get last node of linked list
temp = this->tail;
// iterate linked list
while (temp != NULL)
{
// Display node value
cout << " " << temp->data;
// Visit to prev node
temp = temp->prev;
}
}
}
};
int main()
{
DoublyLinkedList *dll = new DoublyLinkedList();
// 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
type LinkNode struct {
data int
next * LinkNode
prev * LinkNode
}
func getLinkNode(data int) * LinkNode {
var me *LinkNode = &LinkNode {}
me.data = data
me.next = nil
me.prev = nil
return me
}
type DoublyLinkedList struct {
head * LinkNode
tail * LinkNode
}
func getDoublyLinkedList() * DoublyLinkedList {
var me *DoublyLinkedList = &DoublyLinkedList {}
// Set inital value
me.head = nil
me.tail = nil
return me
}
// Insert new node at end position
func(this *DoublyLinkedList) insert(value int) {
// Create a node
var node * LinkNode = getLinkNode(value)
if this.head == nil {
// Add first node
this.head = 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
func(this DoublyLinkedList) display() {
if this.head == nil {
fmt.Println("Empty Linked List")
} else {
fmt.Print("Linked List Head to Tail :")
// Get first node of linked list
var temp * LinkNode = this.head
// iterate linked list
for (temp != nil) {
// Display node value
fmt.Print(" ", temp.data)
// Visit to next node
temp = temp.next
}
fmt.Print("\nLinked List Tail to Head :")
// Get last node of linked list
temp = this.tail
// iterate linked list
for (temp != nil) {
// Display node value
fmt.Print(" ", temp.data)
// Visit to prev node
temp = temp.prev
}
}
}
func main() {
var dll * DoublyLinkedList = getDoublyLinkedList()
// 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 class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
public class DoublyLinkedList
{
public LinkNode head;
public LinkNode tail;
public DoublyLinkedList()
{
// Set inital value
this.head = null;
this.tail = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = 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()
{
if (this.head == null)
{
Console.WriteLine("Empty Linked List");
}
else
{
Console.Write("Linked List Head to Tail :");
// Get first node of linked list
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
Console.Write("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
Console.Write(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
public static void Main(String[] args)
{
DoublyLinkedList dll = new DoublyLinkedList();
// 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
class LinkNode
{
public $data;
public $next;
public $prev;
public function __construct($data)
{
$this->data = $data;
$this->next = NULL;
$this->prev = NULL;
}
}
class DoublyLinkedList
{
public $head;
public $tail;
public function __construct()
{
$this->head = NULL;
$this->tail = NULL;
}
// Insert new node at end position
public function insert($value)
{
// Create a node
$node = new LinkNode($value);
if ($this->head == NULL)
{
// Add first node
$this->head = $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()
{
if ($this->head == NULL)
{
print_r("Empty Linked List".
"\n");
}
else
{
print_r("Linked List Head to Tail :");
// Get first node of linked list
$temp = $this->head;
// iterate linked list
while ($temp != NULL)
{
// Display node value
print_r(" ".strval($temp->data));
// Visit to next node
$temp = $temp->next;
}
print_r("\nLinked List Tail to Head :");
// Get last node of linked list
$temp = $this->tail;
// iterate linked list
while ($temp != NULL)
{
// Display node value
print_r(" ".strval($temp->data));
// Visit to prev node
$temp = $temp->prev;
}
}
}
public static
function main($args)
{
$dll = new DoublyLinkedList();
// 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();
}
}
DoublyLinkedList::main(array());
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
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList
{
constructor()
{
this.head = null;
this.tail = null;
}
// Insert new node at end position
insert(value)
{
// Create a node
var node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = 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()
{
if (this.head == null)
{
console.log("Empty Linked List");
}
else
{
process.stdout.write("Linked List Head to Tail :");
// Get first node of linked list
var temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
process.stdout.write("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
process.stdout.write(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}
function main()
{
var dll = new DoublyLinkedList();
// 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
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList :
def __init__(self) :
self.head = None
self.tail = None
# Insert new node at end position
def insert(self, value) :
# Create a node
node = LinkNode(value)
if (self.head == None) :
# Add first node
self.head = 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) :
if (self.head == None) :
print("Empty Linked List")
else :
print("Linked List Head to Tail :", end = "")
# Get first node of linked list
temp = self.head
# iterate linked list
while (temp != None) :
# Display node value
print(" ", temp.data, end = "")
# Visit to next node
temp = temp.next
print("\nLinked List Tail to Head :", end = "")
# Get last node of linked list
temp = self.tail
# iterate linked list
while (temp != None) :
# Display node value
print(" ", temp.data, end = "")
# Visit to prev node
temp = temp.prev
def main() :
dll = DoublyLinkedList()
# 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
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next, :prev
attr_accessor :data, :next, :prev
def initialize(data)
self.data = data
self.next = nil
self.prev = nil
end
end
class DoublyLinkedList
# Define the accessor and reader of class DoublyLinkedList
attr_reader :head, :tail
attr_accessor :head, :tail
def initialize()
self.head = nil
self.tail = nil
end
# Insert new node at end position
def insert(value)
# Create a node
node = LinkNode.new(value)
if (self.head == nil)
# Add first node
self.head = 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()
if (self.head == nil)
print("Empty Linked List", "\n")
else
print("Linked List Head to Tail :")
# Get first node of linked list
temp = self.head
# iterate linked list
while (temp != nil)
# Display node value
print(" ", temp.data)
# Visit to next node
temp = temp.next
end
print("\nLinked List Tail to Head :")
# Get last node of linked list
temp = self.tail
# iterate linked list
while (temp != nil)
# Display node value
print(" ", temp.data)
# Visit to prev node
temp = temp.prev
end
end
end
end
def main()
dll = DoublyLinkedList.new()
# 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
class LinkNode(var data: Int,
var next: LinkNode,
var prev: LinkNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
class DoublyLinkedList(var head: LinkNode,
var tail: LinkNode)
{
def this()
{
this(null, null);
}
// Insert new node at end position
def insert(value: Int): Unit = {
// Create a node
var node: LinkNode = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = 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 = {
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Head to Tail :");
// Get first node of linked list
var temp: LinkNode = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
print(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var dll: DoublyLinkedList = new DoublyLinkedList();
// 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
class LinkNode
{
var data: Int;
var next: LinkNode? ;
var prev: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
self.prev = nil;
}
}
class DoublyLinkedList
{
var head: LinkNode? ;
var tail: LinkNode? ;
init()
{
self.head = nil;
self.tail = nil;
}
// Insert new node at end position
func insert(_ value: Int)
{
// Create a node
let node: LinkNode = LinkNode(value);
if (self.head == nil)
{
// Add first node
self.head = 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()
{
if (self.head == nil)
{
print("Empty Linked List");
}
else
{
print("Linked List Head to Tail :", terminator: "");
// Get first node of linked list
var temp: LinkNode? = self.head;
// iterate linked list
while (temp != nil)
{
// Display node value
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
print("\nLinked List Tail to Head :", terminator: "");
// Get last node of linked list
temp = self.tail;
// iterate linked list
while (temp != nil)
{
// Display node value
print(" ", temp!.data, terminator: "");
// Visit to prev node
temp = temp!.prev;
}
}
}
}
func main()
{
let dll: DoublyLinkedList = DoublyLinkedList();
// 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
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
var prev: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList
{
var head: LinkNode ? ;
var tail: LinkNode ? ;
constructor()
{
this.head = null;
this.tail = null;
}
// Insert new node at end position
fun insert(value: Int): Unit
{
// Create a node
val node: LinkNode = LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = 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
{
if (this.head == null)
{
println("Empty Linked List");
}
else
{
print("Linked List Head to Tail :");
// Get first node of linked list
var temp: LinkNode ? = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
print(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
}
fun main(args: Array < String > ): Unit
{
val dll: DoublyLinkedList = DoublyLinkedList();
// 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
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