Print node in forward and backward direction in doubly linked list

Here given code implementation process.

// C Program
// Print node in forward and backward direction in doubly linked list
#include <stdio.h>

#include <stdlib.h>

struct LinkNode
{
    int key;
    struct LinkNode *back;
    struct LinkNode *next;
};

struct DoublyLL
{
    struct LinkNode *front;
    struct LinkNode *rear;
};
// Returns a new linked list
struct DoublyLL *createLinkedList()
{
    struct DoublyLL *dll = 
      (struct DoublyLL *) malloc(sizeof(struct DoublyLL));
    if (dll == NULL)
    {
        printf("\n Memory overflow , When creating a new linked list");
    }
    else
    {
        // Set initial value of linked list
        dll->front = NULL;
        dll->rear = NULL;
    }
    return dll;
}
// Returns a new node of linked list
struct LinkNode *newNode(int key, struct LinkNode *back)
{
    // Create dynamic node
    struct LinkNode *node = 
      (struct LinkNode *) malloc(sizeof(struct LinkNode));
    if (node != NULL)
    {
        node->key = key;
        node->back = back;
        node->next = NULL;
    }
    else
    {
        //This is indicates, 
      	// segmentation fault or memory overflow problem
        printf("Memory Overflow\n");
    }
    //return new node
    return node;
}
// Handles the request to add new node 
// at the end of linked list
void addNode(struct DoublyLL *dll, int data)
{
    // Create dynamic node
    struct LinkNode *node = newNode(data, dll->rear);
    if (dll->front == NULL)
    {
        dll->front = node;
        dll->rear = node;
    }
    else
    {
        dll->rear->next = node;
    }
    dll->rear = node;
}

// Print linked list node from left to right 
// forward direction
void forwardDirection(struct DoublyLL *dll)
{
    if (dll->front == NULL)
    {
        printf("\n Empty linked list\n");
        return;
    }
    printf("\n Forward Direction Nodes \n");

    struct LinkNode *temp = dll->front;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf("  %d",temp->key);
        // Visit to next node
        temp = temp->next;
    }

}
// Print linked list node from right to left 
// backward direction
void backwardDirection(struct DoublyLL *dll)
{
    if (dll->rear == NULL)
    {
        printf("\n Empty linked list\n");
        return;
    }
     printf("\n Backward Direction Nodes \n");

    // In case last node unknown then first find last node
    struct LinkNode *temp = dll->rear;

    // iterating linked list elements
    while (temp != NULL)
    {
        printf("  %d",temp->key);
        // Visit to previous node
        temp = temp->back;
    }

}
int main()
{
    struct DoublyLL *dll = createLinkedList();
    // Add nodes
    addNode(dll, 24);
    addNode(dll, 4);
    addNode(dll, 21);
    addNode(dll, 10);
    addNode(dll, 13);
    addNode(dll, 15);
    addNode(dll, 28);
	// Constructing linked list
    // NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
  	//         ↑                            ↑
    //         head                        rear
    forwardDirection(dll);
	backwardDirection(dll);
  
    return 0;
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
/* 
  Java Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode
{
	public int key;
	public LinkNode next;
	public LinkNode back;
	public LinkNode(int key)
	{
		// Setup initial values of linked list node
		this.key = key;
		this.next = null;
		this.back = null;
	}
}
class DoublyLL
{
	public LinkNode front;
	public LinkNode rear;
	public DoublyLL()
	{
		// Set initial default value
		this.front = null;
		this.rear = null;
	}
	public void addNode(int value)
	{
		// Create new node
		LinkNode node = new LinkNode(value);
		if (this.front == null)
		{
			this.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = this.rear;
			this.rear.next = node;
		}
		// New last node
		this.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	public void forwardDirection()
	{
		if (this.front == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		System.out.print("\n Forward Direction Nodes \n");
		LinkNode temp = this.front;
		// iterating linked list elements
		while (temp != null)
		{
			System.out.print("  " + temp.key);
			// Visit to next node
			temp = temp.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	public void backwardDirection()
	{
		if (this.rear == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		System.out.print("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		LinkNode temp = this.rear;
		// iterating linked list elements
		while (temp != null)
		{
			System.out.print("  " + temp.key);
			// Visit to previous node
			temp = temp.back;
		}
	}
	public static void main(String[] args)
	{
		DoublyLL dll = new DoublyLL();
		// Add nodes
		dll.addNode(24);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// Constructing linked list
		// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
		//         ↑                            ↑
		//         head                        rear
		dll.forwardDirection();
		dll.backwardDirection();
	}
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program 
  Extract leaves of a binary tree in a doubly linked list
*/

class LinkNode
{
	public: 
    int key;
	LinkNode *next;
	LinkNode *back;
	LinkNode(int key)
	{
		// Setup initial values of linked list node
		this->key = key;
		this->next = NULL;
		this->back = NULL;
	}
};
class DoublyLL
{
	public: LinkNode *front;
	LinkNode *rear;
	DoublyLL()
	{
		this->front = NULL;
		this->rear = NULL;
	}
	void addNode(int value)
	{
		// Create new node
		LinkNode *node = new LinkNode(value);
		if (this->front == NULL)
		{
			this->front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node->back = this->rear;
			this->rear->next = node;
		}
		// New last node
		this->rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	void forwardDirection()
	{
		if (this->front == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		cout << "\n Forward Direction Nodes \n";
		LinkNode *temp = this->front;
		// iterating linked list elements
		while (temp != NULL)
		{
			cout << "  " << temp->key;
			// Visit to next node
			temp = temp->next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	void backwardDirection()
	{
		if (this->rear == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		cout << "\n Backward Direction Nodes \n";
		// In case last node unknown then first find last node
		LinkNode *temp = this->rear;
		// iterating linked list elements
		while (temp != NULL)
		{
			cout << "  " << temp->key;
			// Visit to previous node
			temp = temp->back;
		}
	}
};
int main()
{
	DoublyLL *dll = new DoublyLL();
	// Add nodes
	dll->addNode(24);
	dll->addNode(4);
	dll->addNode(21);
	dll->addNode(10);
	dll->addNode(13);
	dll->addNode(15);
	dll->addNode(28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	dll->forwardDirection();
	dll->backwardDirection();
	return 0;
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
// Include namespace system
using System;
/* 
  Csharp Program 
  Extract leaves of a binary tree in a doubly linked list
*/
public class LinkNode
{
	public int key;
	public LinkNode next;
	public LinkNode back;
	public LinkNode(int key)
	{
		// Setup initial values of linked list node
		this.key = key;
		this.next = null;
		this.back = null;
	}
}
public class DoublyLL
{
	public LinkNode front;
	public LinkNode rear;
	public DoublyLL()
	{
		// Set initial default value
		this.front = null;
		this.rear = null;
	}
	public void addNode(int value)
	{
		// Create new node
		LinkNode node = new LinkNode(value);
		if (this.front == null)
		{
			this.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = this.rear;
			this.rear.next = node;
		}
		// New last node
		this.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	public void forwardDirection()
	{
		if (this.front == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		Console.Write("\n Forward Direction Nodes \n");
		LinkNode temp = this.front;
		// iterating linked list elements
		while (temp != null)
		{
			Console.Write("  " + temp.key);
			// Visit to next node
			temp = temp.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	public void backwardDirection()
	{
		if (this.rear == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		Console.Write("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		LinkNode temp = this.rear;
		// iterating linked list elements
		while (temp != null)
		{
			Console.Write("  " + temp.key);
			// Visit to previous node
			temp = temp.back;
		}
	}
	public static void Main(String[] args)
	{
		DoublyLL dll = new DoublyLL();
		// Add nodes
		dll.addNode(24);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// Constructing linked list
		// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
		//         ↑                            ↑
		//         head                        rear
		dll.forwardDirection();
		dll.backwardDirection();
	}
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
<?php
/* 
  Php Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode
{
	public $key;
	public $next;
	public $back;
	public	function __construct($key)
	{
		// Setup initial values of linked list node
		$this->key = $key;
		$this->next = NULL;
		$this->back = NULL;
	}
}
class DoublyLL
{
	public $front;
	public $rear;
	public	function __construct()
	{
		$this->front = NULL;
		$this->rear = NULL;
	}
	public	function addNode($value)
	{
		// Create new node
		$node = new LinkNode($value);
		if ($this->front == NULL)
		{
			$this->front = $node;
		}
		else
		{
			// Add Node at the end of linked list
			$node->back = $this->rear;
			$this->rear->next = $node;
		}
		// New last node
		$this->rear = $node;
	}
	// Print linked list node from left to right 
	// forward direction
	public	function forwardDirection()
	{
		if ($this->front == NULL)
		{
			echo("\n Empty linked list\n");
			return;
		}
		echo("\n Forward Direction Nodes \n");
		$temp = $this->front;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo("  ".$temp->key);
			// Visit to next node
			$temp = $temp->next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	public	function backwardDirection()
	{
		if ($this->rear == NULL)
		{
			echo("\n Empty linked list\n");
			return;
		}
		echo("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		$temp = $this->rear;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo("  ".$temp->key);
			// Visit to previous node
			$temp = $temp->back;
		}
	}
}

function main()
{
	$dll = new DoublyLL();
	// Add nodes
	$dll->addNode(24);
	$dll->addNode(4);
	$dll->addNode(21);
	$dll->addNode(10);
	$dll->addNode(13);
	$dll->addNode(15);
	$dll->addNode(28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	$dll->forwardDirection();
	$dll->backwardDirection();
}
main();

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
/* 
  Node JS Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode
{
	constructor(key)
	{
		// Setup initial values of linked list node
		this.key = key;
		this.next = null;
		this.back = null;
	}
}
class DoublyLL
{
	constructor()
	{
		this.front = null;
		this.rear = null;
	}
	addNode(value)
	{
		// Create new node
		var node = new LinkNode(value);
		if (this.front == null)
		{
			this.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = this.rear;
			this.rear.next = node;
		}
		// New last node
		this.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	forwardDirection()
	{
		if (this.front == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		process.stdout.write("\n Forward Direction Nodes \n");
		var temp = this.front;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write("  " + temp.key);
			// Visit to next node
			temp = temp.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	backwardDirection()
	{
		if (this.rear == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		process.stdout.write("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		var temp = this.rear;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write("  " + temp.key);
			// Visit to previous node
			temp = temp.back;
		}
	}
}

function main()
{
	var dll = new DoublyLL();
	// Add nodes
	dll.addNode(24);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	dll.forwardDirection();
	dll.backwardDirection();
}
main();

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
#  Python 3 Program 
#  Extract leaves of a binary tree in a doubly linked list
class LinkNode :
	def __init__(self, key) :
		#  Setup initial values of linked list node
		self.key = key
		self.next = None
		self.back = None
	

class DoublyLL :
	def __init__(self) :
		self.front = None
		self.rear = None
	
	def addNode(self, value) :
		#  Create new node
		node = LinkNode(value)
		if (self.front == None) :
			self.front = node
		else :
			#  Add Node at the end of linked list
			node.back = self.rear
			self.rear.next = node
		
		#  New last node
		self.rear = node
	
	#  Print linked list node from left to right 
	#  forward direction
	def forwardDirection(self) :
		if (self.front == None) :
			print("\n Empty linked list")
			return
		
		print("\n Forward Direction Nodes ")
		temp = self.front
		#  iterating linked list elements
		while (temp != None) :
			print("  ", temp.key, end = "")
			#  Visit to next node
			temp = temp.next
		
	
	#  Print linked list node from right to left 
	#  backward direction
	def backwardDirection(self) :
		if (self.rear == None) :
			print("\n Empty linked list")
			return
		
		print("\n Backward Direction Nodes ")
		#  In case last node unknown then first find last node
		temp = self.rear
		#  iterating linked list elements
		while (temp != None) :
			print("  ", temp.key, end = "")
			#  Visit to previous node
			temp = temp.back
		
	

def main() :
	dll = DoublyLL()
	#  Add nodes
	dll.addNode(24)
	dll.addNode(4)
	dll.addNode(21)
	dll.addNode(10)
	dll.addNode(13)
	dll.addNode(15)
	dll.addNode(28)
	#  Constructing linked list
	#  NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	#          ↑                            ↑
	#          head                        rear
	dll.forwardDirection()
	dll.backwardDirection()

if __name__ == "__main__": main()

input

 Forward Direction Nodes
   24   4   21   10   13   15   28
 Backward Direction Nodes
   28   15   13   10   21   4   24
#  Ruby Program 
#  Extract leaves of a binary tree in a doubly linked list
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :key, :next, :back
	attr_accessor :key, :next, :back
	def initialize(key) 
		#  Setup initial values of linked list node
		self.key = key
		self.next = nil
		self.back = nil
	end

end

class DoublyLL 
	# Define the accessor and reader of class DoublyLL
	attr_reader :front, :rear
	attr_accessor :front, :rear
	def initialize() 
		self.front = nil
		self.rear = nil
	end

	def addNode(value) 
		#  Create new node
		node = LinkNode.new(value)
		if (self.front == nil) 
			self.front = node
		else
 
			#  Add Node at the end of linked list
			node.back = self.rear
			self.rear.next = node
		end

		#  New last node
		self.rear = node
	end

	#  Print linked list node from left to right 
	#  forward direction
	def forwardDirection() 
		if (self.front == nil) 
			print("\n Empty linked list\n")
			return
		end

		print("\n Forward Direction Nodes \n")
		temp = self.front
		#  iterating linked list elements
		while (temp != nil) 
			print("  ", temp.key)
			#  Visit to next node
			temp = temp.next
		end

	end

	#  Print linked list node from right to left 
	#  backward direction
	def backwardDirection() 
		if (self.rear == nil) 
			print("\n Empty linked list\n")
			return
		end

		print("\n Backward Direction Nodes \n")
		#  In case last node unknown then first find last node
		temp = self.rear
		#  iterating linked list elements
		while (temp != nil) 
			print("  ", temp.key)
			#  Visit to previous node
			temp = temp.back
		end

	end

end

def main() 
	dll = DoublyLL.new()
	#  Add nodes
	dll.addNode(24)
	dll.addNode(4)
	dll.addNode(21)
	dll.addNode(10)
	dll.addNode(13)
	dll.addNode(15)
	dll.addNode(28)
	#  Constructing linked list
	#  NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	#          ↑                            ↑
	#          head                        rear
	dll.forwardDirection()
	dll.backwardDirection()
end

main()

input

 Forward Direction Nodes 
  24  4  21  10  13  15  28
 Backward Direction Nodes 
  28  15  13  10  21  4  24
/* 
  Scala Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode(var key: Int,
	var next: LinkNode,
		var back: LinkNode)
{
	def this(key: Int)
	{
		// Setup initial values of linked list node
		this(key,null,null);
	}
}
class DoublyLL(var front: LinkNode,
	var rear: LinkNode)
{
	def this()
	{
		this(null,null);
	}
	def addNode(value: Int): Unit = {
		// Create new node
		var node: LinkNode = new LinkNode(value);
		if (this.front == null)
		{
			this.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = this.rear;
			this.rear.next = node;
		}
		// New last node
		this.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	def forwardDirection(): Unit = {
		if (this.front == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print("\n Forward Direction Nodes \n");
		var temp: LinkNode = this.front;
		// iterating linked list elements
		while (temp != null)
		{
			print("  " + temp.key);
			// Visit to next node
			temp = temp.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	def backwardDirection(): Unit = {
		if (this.rear == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		var temp: LinkNode = this.rear;
		// iterating linked list elements
		while (temp != null)
		{
			print("  " + temp.key);
			// Visit to previous node
			temp = temp.back;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var dll: DoublyLL = new DoublyLL();
		// Add nodes
		dll.addNode(24);
		dll.addNode(4);
		dll.addNode(21);
		dll.addNode(10);
		dll.addNode(13);
		dll.addNode(15);
		dll.addNode(28);
		// Constructing linked list
		// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
		//         ↑                            ↑
		//         head                        rear
		dll.forwardDirection();
		dll.backwardDirection();
	}
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24
/* 
  Swift 4 Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode
{
	var key: Int;
	var next: LinkNode? ;
	var back: LinkNode? ;
	init(_ key: Int)
	{
		// Setup initial values of linked list node
		self.key = key;
		self.next = nil;
		self.back = nil;
	}
}
class DoublyLL
{
	var front: LinkNode? ;
	var rear: LinkNode? ;
	init()
	{
		self.front = nil;
		self.rear = nil;
	}
	func addNode(_ value: Int)
	{
		// Create new node
		let node = LinkNode(value);
		if (self.front == nil)
		{
			self.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = self.rear;
			self.rear!.next = node;
		}
		// New last node
		self.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	func forwardDirection()
	{
		if (self.front == nil)
		{
			print("\n Empty linked list");
			return;
		}
		print("\n Forward Direction Nodes ");
		var temp = self.front;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("  ", temp!.key, terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	func backwardDirection()
	{
		if (self.rear == nil)
		{
			print("\n Empty linked list");
			return;
		}
		print("\n Backward Direction Nodes ");
		// In case last node unknown then first find last node
		var temp = self.rear;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("  ", temp!.key, terminator: "");
			// Visit to previous node
			temp = temp!.back;
		}
	}
}
func main()
{
	let dll = DoublyLL();
	// Add nodes
	dll.addNode(24);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	dll.forwardDirection();
	dll.backwardDirection();
}
main();

input

 Forward Direction Nodes
   24   4   21   10   13   15   28
 Backward Direction Nodes
   28   15   13   10   21   4   24
/* 
  Kotlin Program 
  Extract leaves of a binary tree in a doubly linked list
*/
class LinkNode
{
	var key: Int;
	var next: LinkNode ? ;
	var back: LinkNode ? ;
	constructor(key: Int)
	{
		// Setup initial values of linked list node
		this.key = key;
		this.next = null;
		this.back = null;
	}
}
class DoublyLL
{
	var front: LinkNode ? ;
	var rear: LinkNode ? ;
	constructor()
	{
		this.front = null;
		this.rear = null;
	}
	fun addNode(value: Int): Unit
	{
		// Create new node
		val node: LinkNode = LinkNode(value);
		if (this.front == null)
		{
			this.front = node;
		}
		else
		{
			// Add Node at the end of linked list
			node.back = this.rear;
			this.rear?.next = node;
		}
		// New last node
		this.rear = node;
	}
	// Print linked list node from left to right 
	// forward direction
	fun forwardDirection(): Unit
	{
		if (this.front == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print("\n Forward Direction Nodes \n");
		var temp: LinkNode ? = this.front;
		// iterating linked list elements
		while (temp != null)
		{
			print("  " + temp.key);
			// Visit to next node
			temp = temp.next;
		}
	}
	// Print linked list node from right to left 
	// backward direction
	fun backwardDirection(): Unit
	{
		if (this.rear == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		print("\n Backward Direction Nodes \n");
		// In case last node unknown then first find last node
		var temp: LinkNode ? = this.rear;
		// iterating linked list elements
		while (temp != null)
		{
			print("  " + temp.key);
			// Visit to previous node
			temp = temp.back;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val dll: DoublyLL = DoublyLL();
	// Add nodes
	dll.addNode(24);
	dll.addNode(4);
	dll.addNode(21);
	dll.addNode(10);
	dll.addNode(13);
	dll.addNode(15);
	dll.addNode(28);
	// Constructing linked list
	// NULL ← 24 ⟷ 4 ⟷ 21 ⟷ 10 ⟷ 13 ⟷ 15 ⟷ 28 → NULL
	//         ↑                            ↑
	//         head                        rear
	dll.forwardDirection();
	dll.backwardDirection();
}

input

 Forward Direction Nodes
  24  4  21  10  13  15  28
 Backward Direction Nodes
  28  15  13  10  21  4  24

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