Implement queue using doubly linked list

Here given code implementation process.

/*
    C Program 
    Implement queue using doubly linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Create structure of Doubly Linked List Node
struct QNode
{
	int data;
	struct QNode *next;
	struct QNode *prev;
};
struct MyQueue
{
	struct QNode *front;
	struct QNode *rear;
	int size;
};
// Returns a new queue 
struct MyQueue *newMyQueue()
{
	struct MyQueue *q = (struct MyQueue *) malloc(sizeof(struct MyQueue));
	if (q == NULL)
	{
		printf("\n Memory overflow , When creating a new Queue");
	}
	else
	{
		q->front = NULL;
		q->rear = NULL;
		q->size = 0;
	}
	return q;
}
// Add a node into queue
void enqueue(struct MyQueue *q, int data)
{
	//Create a dynamic node
	struct QNode *node = (struct QNode *) malloc(sizeof(struct QNode));
	if (node == NULL)
	{
		printf("\n Memory overflow , When creating a new Queue Node");
	}
	else
	{
		// Set node value
		node->data = data;
		node->next = NULL;
		node->prev = q->rear;
		if (q->front == NULL)
		{
			// When adding a first node of queue
			q->front = node;
			q->size = 1;
		}
		else
		{
			q->rear->next = node;
			q->size = q->size + 1;
		}
		q->rear = node;
	}
}
int isEmpty(struct MyQueue *q)
{
	if (q->size == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
// Get a front element of queue
int peek(struct MyQueue *q)
{
	if (isEmpty(q) == 1)
	{
		// When stack is empty
		return -1;
	}
	else
	{
		return q->front->data;
	}
}
int isSize(struct MyQueue *q)
{
	return q->size;
}
// Remove a front node of a queue
int dequeue(struct MyQueue *q)
{
	if (isEmpty(q) == 1)
	{
		// When queue is empty
		return -1;
	}
	else
	{
		int data = peek(q);
		struct QNode *temp = q->front;
		if (q->front == q->rear)
		{
			// When queue contains only one node
			q->rear = NULL;
			q->front = NULL;
		}
		else
		{
			q->front = q->front->next;
			q->front->prev = NULL;
		}
		// Change queue size
		q->size--;
		free(temp);
		return data;
	}
}
// Print elements of queue
void printQdata(struct MyQueue *q)
{
	struct QNode *node = q->front;
	printf("\n Queue Element \n");
	while (node != NULL)
	{
		printf(" %d", node->data);
		node = node->next;
	}
	printf("\n");
}
int main(int argc, char
	const *argv[])
{
	struct MyQueue *q = newMyQueue();
	// Add queue element
	enqueue(q, 1);
	enqueue(q, 2);
	enqueue(q, 3);
	enqueue(q, 4);
	enqueue(q, 5);
	printQdata(q);
	printf(" Size : %d", isSize(q));
	// Remove queue element
	printf("\n Dequeue Node : %d", dequeue(q));
	printf("\n Dequeue Node : %d", dequeue(q));
	printf("\n Dequeue Node : %d", dequeue(q));
	printQdata(q);
	printf(" Size : %d", isSize(q));
	return 0;
}

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
/*
    Java Program 
    Implement queue using doubly linked list
*/
class QNode
{
	public int data;
	public QNode next;
	public QNode prev;
	public QNode(int data, QNode prev)
	{
		this.data = data;
		this.next = null;
		this.prev = prev;
	}
}
public class MyQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public MyQueue()
	{
		this.front = null;
		this.rear = null;
	}
	// Add a node into queue
	public void enqueue(int data)
	{
		//Create a dynamic node
		QNode node = new QNode(data, this.rear);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.size = 1;
		}
		else
		{
			this.rear.next = node;
			this.size = this.size + 1;
		}
		this.rear = node;
	}
	public boolean isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	public int peek()
	{
		if (this.isEmpty() == true)
		{
			System.out.print("\n Empty Queue \n");
			// When stack is empty
			return -1;
		}
		else
		{
			return this.front.data;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	public int dequeue()
	{
		if (this.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			int data = this.peek();
			QNode temp = this.front;
			if (this.front == this.rear)
			{
				// When queue contains only one node
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
				this.front.prev = null;
			}
			// Change queue size
			this.size--;
			return data;
		}
	}
	// Print elements of queue
	public void printQdata()
	{
		QNode node = this.front;
		System.out.print("\n Queue Element \n");
		while (node != null)
		{
			System.out.print(" " + node.data);
			node = node.next;
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyQueue q = new MyQueue();
		// Add queue element
		q.enqueue(1);
		q.enqueue(2);
		q.enqueue(3);
		q.enqueue(4);
		q.enqueue(5);
		q.printQdata();
		System.out.print(" Size : " + q.isSize());
		// Remove queue element
		System.out.print("\n Dequeue Node : " + q.dequeue());
		System.out.print("\n Dequeue Node : " + q.dequeue());
		System.out.print("\n Dequeue Node : " + q.dequeue());
		q.printQdata();
		System.out.print(" Size : " + q.isSize());
	}
}

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program 
    Implement queue using doubly linked list
*/
class QNode
{
	public: int data;
	QNode *next;
	QNode *prev;
	QNode(int data, QNode *prev)
	{
		this->data = data;
		this->next = NULL;
		this->prev = prev;
	}
};
class MyQueue
{
	public: QNode *front;
	QNode *rear;
	int size;
	MyQueue()
	{
		this->front = NULL;
		this->rear = NULL;
	}
	// Add a node into queue
	void enqueue(int data)
	{
		//Create a dynamic node
		QNode *node = new QNode(data, this->rear);
		if (this->front == NULL)
		{
			// When adding a first node of queue
			this->front = node;
			this->size = 1;
		}
		else
		{
			this->rear->next = node;
			this->size = this->size + 1;
		}
		this->rear = node;
	}
	bool isEmpty()
	{
		if (this->size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	int peek()
	{
		if (this->isEmpty() == true)
		{
			cout << "\n Empty Queue \n";
			// When stack is empty
			return -1;
		}
		else
		{
			return this->front->data;
		}
	}
	int isSize()
	{
		return this->size;
	}
	// Remove a front node of a queue
	int dequeue()
	{
		if (this->isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			int data = this->peek();
			QNode *temp = this->front;
			if (this->front == this->rear)
			{
				// When queue contains only one node
				this->rear = NULL;
				this->front = NULL;
			}
			else
			{
				this->front = this->front->next;
				this->front->prev = NULL;
			}
			// Change queue size
			this->size--;
          	delete temp;
			return data;
		}
	}
	// Print elements of queue
	void printQdata()
	{
		QNode *node = this->front;
		cout << "\n Queue Element \n";
		while (node != NULL)
		{
			cout << " " << node->data;
			node = node->next;
		}
		cout << "\n";
	}
};
int main()
{
	MyQueue q = MyQueue();
	// Add queue element
	q.enqueue(1);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(4);
	q.enqueue(5);
	q.printQdata();
	cout << " Size : " << q.isSize();
	// Remove queue element
	cout << "\n Dequeue Node : " << q.dequeue();
	cout << "\n Dequeue Node : " << q.dequeue();
	cout << "\n Dequeue Node : " << q.dequeue();
	q.printQdata();
	cout << " Size : " << q.isSize();
	return 0;
}

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
// Include namespace system
using System;
/*
    C# Program 
    Implement queue using doubly linked list
*/
public class QNode
{
	public int data;
	public QNode next;
	public QNode prev;
	public QNode(int data, QNode prev)
	{
		this.data = data;
		this.next = null;
		this.prev = prev;
	}
}
public class MyQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public MyQueue()
	{
		this.front = null;
		this.rear = null;
	}
	// Add a node into queue
	public void enqueue(int data)
	{
		//Create a dynamic node
		QNode node = new QNode(data, this.rear);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.size = 1;
		}
		else
		{
			this.rear.next = node;
			this.size = this.size + 1;
		}
		this.rear = node;
	}
	public Boolean isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	public int peek()
	{
		if (this.isEmpty() == true)
		{
			Console.Write("\n Empty Queue \n");
			// When stack is empty
			return -1;
		}
		else
		{
			return this.front.data;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	public int dequeue()
	{
		if (this.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			int data = this.peek();
			if (this.front == this.rear)
			{
				// When queue contains only one node
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
				this.front.prev = null;
			}
			// Change queue size
			this.size--;
			return data;
		}
	}
	// Print elements of queue
	public void printQdata()
	{
		QNode node = this.front;
		Console.Write("\n Queue Element \n");
		while (node != null)
		{
			Console.Write("  " + node.data);
			node = node.next;
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyQueue q = new MyQueue();
		// Add queue element
		q.enqueue(1);
		q.enqueue(2);
		q.enqueue(3);
		q.enqueue(4);
		q.enqueue(5);
		q.printQdata();
		Console.Write(" Size : " + q.isSize());
		// Remove queue element
		Console.Write("\n Dequeue Node : " + q.dequeue());
		Console.Write("\n Dequeue Node : " + q.dequeue());
		Console.Write("\n Dequeue Node : " + q.dequeue());
		q.printQdata();
		Console.Write(" Size : " + q.isSize());
	}
}

Output

 Queue Element
  1  2  3  4  5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
  4  5
 Size : 2
<?php
/*
    Php Program 
    Implement queue using doubly linked list
*/
class QNode
{
	public $data;
	public $next;
	public $prev;

	function __construct($data, $prev)
	{
		$this->data = $data;
		$this->next = null;
		$this->prev = $prev;
	}
}
class MyQueue
{
	public $front;
	public $rear;
	public $size;

	function __construct()
	{
		$this->front = null;
		$this->rear = null;
	}
	// Add a node into queue
	public	function enqueue($data)
	{
		//Create a dynamic node
		$node = new QNode($data, $this->rear);
		if ($this->front == null)
		{
			// When adding a first node of queue
			$this->front = $node;
			$this->size = 1;
		}
		else
		{
			$this->rear->next = $node;
			$this->size = $this->size + 1;
		}
		$this->rear = $node;
	}
	public	function isEmpty()
	{
		if ($this->size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	public	function peek()
	{
		if ($this->isEmpty() == true)
		{
			echo "\n Empty Queue \n";
			// When stack is empty
			return -1;
		}
		else
		{
			return $this->front->data;
		}
	}
	public	function isSize()
	{
		return $this->size;
	}
	// Remove a front node of a queue
	public	function dequeue()
	{
		if ($this->isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			$data = $this->peek();
			if ($this->front == $this->rear)
			{
				// When queue contains only one node
				$this->rear = null;
				$this->front = null;
			}
			else
			{
				$this->front = $this->front->next;
				$this->front->prev = null;
			}
			// Change queue size
			$this->size--;
			return $data;
		}
	}
	// Print elements of queue
	public	function printQdata()
	{
		$node = $this->front;
		echo "\n Queue Element \n";
		while ($node != null)
		{
			echo " ". $node->data;
			$node = $node->next;
		}
		echo "\n";
	}
}

function main()
{
	$q = new MyQueue();
	// Add queue element
	$q->enqueue(1);
	$q->enqueue(2);
	$q->enqueue(3);
	$q->enqueue(4);
	$q->enqueue(5);
	$q->printQdata();
	echo " Size : ". $q->isSize();
	// Remove queue element
	echo "\n Dequeue Node : ". $q->dequeue();
	echo "\n Dequeue Node : ". $q->dequeue();
	echo "\n Dequeue Node : ". $q->dequeue();
	$q->printQdata();
	echo " Size : ". $q->isSize();
}
main();

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
/*
    Node Js Program 
    Implement queue using doubly linked list
*/
class QNode
{
	constructor(data, prev)
	{
		this.data = data;
		this.next = null;
		this.prev = prev;
	}
}
class MyQueue
{
	constructor()
	{
		this.front = null;
		this.rear = null;
	}
	// Add a node into queue
	enqueue(data)
	{
		//Create a dynamic node
		var node = new QNode(data, this.rear);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.size = 1;
		}
		else
		{
			this.rear.next = node;
			this.size = this.size + 1;
		}
		this.rear = node;
	}
	isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	peek()
	{
		if (this.isEmpty() == true)
		{
			process.stdout.write("\n Empty Queue \n");
			// When stack is empty
			return -1;
		}
		else
		{
			return this.front.data;
		}
	}
	isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	dequeue()
	{
		if (this.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			var data = this.peek();
			if (this.front == this.rear)
			{
				// When queue contains only one node
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
				this.front.prev = null;
			}
			// Change queue size
			this.size--;
			return data;
		}
	}
	// Print elements of queue
	printQdata()
	{
		var node = this.front;
		process.stdout.write("\n Queue Element \n");
		while (node != null)
		{
			process.stdout.write(" " + node.data);
			node = node.next;
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var q = new MyQueue();
	// Add queue element
	q.enqueue(1);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(4);
	q.enqueue(5);
	q.printQdata();
	process.stdout.write(" Size : " + q.isSize());
	// Remove queue element
	process.stdout.write("\n Dequeue Node : " + q.dequeue());
	process.stdout.write("\n Dequeue Node : " + q.dequeue());
	process.stdout.write("\n Dequeue Node : " + q.dequeue());
	q.printQdata();
	process.stdout.write(" Size : " + q.isSize());
}
main();

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
#  Python 3 Program 
#  Implement queue using doubly linked list

class QNode :
	
	def __init__(self, data, prev) :
		self.data = data
		self.next = None
		self.prev = prev
	

class MyQueue :
	
	def __init__(self) :
		self.front = None
		self.rear = None
	
	#  Add a node into queue
	def enqueue(self, data) :
		# Create a dynamic node
		node = QNode(data, self.rear)
		if (self.front == None) :
			#  When adding a first node of queue
			self.front = node
			self.size = 1
		else :
			self.rear.next = node
			self.size = self.size + 1
		
		self.rear = node
	
	def isEmpty(self) :
		if (self.size == 0) :
			return True
		else :
			return False
		
	
	#  Get a front element of queue
	def peek(self) :
		if (self.isEmpty() == True) :
			print("\n Empty Queue ")
			#  When stack is empty
			return -1
		else :
			return self.front.data
		
	
	def isSize(self) :
		return self.size
	
	#  Remove a front node of a queue
	def dequeue(self) :
		if (self.isEmpty() == True) :
			#  When queue is empty
			return -1
		else :
			data = self.peek()
			if (self.front == self.rear) :
				#  When queue contains only one node
				self.rear = None
				self.front = None
			else :
				self.front = self.front.next
				self.front.prev = None
			
			#  Change queue size
			self.size -= 1
			return data
		
	
	#  Print elements of queue
	def printQdata(self) :
		node = self.front
		print("\n Queue Element ")
		while (node != None) :
			print(" ", node.data, end = "")
			node = node.next
		
		print(end = "\n")
	

def main() :
	q = MyQueue()
	#  Add queue element
	q.enqueue(1)
	q.enqueue(2)
	q.enqueue(3)
	q.enqueue(4)
	q.enqueue(5)
	q.printQdata()
	print(" Size : ", q.isSize(), end = "")
	#  Remove queue element
	print("\n Dequeue Node : ", q.dequeue(), end = "")
	print("\n Dequeue Node : ", q.dequeue(), end = "")
	print("\n Dequeue Node : ", q.dequeue(), end = "")
	q.printQdata()
	print(" Size : ", q.isSize(), end = "")

if __name__ == "__main__": main()

Output

 Queue Element
  1  2  3  4  5
 Size :  5
 Dequeue Node :  1
 Dequeue Node :  2
 Dequeue Node :  3
 Queue Element
  4  5
 Size :  2
#  Ruby Program 
#  Implement queue using doubly linked list

class QNode  
	# Define the accessor and reader of class QNode  
	attr_reader :data, :next, :prev
	attr_accessor :data, :next, :prev
 
	
	def initialize(data, prev) 
		self.data = data
		self.next = nil
		self.prev = prev
	end

end

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

	#  Add a node into queue
	def enqueue(data) 
		# Create a dynamic node
		node = QNode.new(data, self.rear)
		if (self.front == nil) 
			#  When adding a first node of queue
			self.front = node
			self.size = 1
		else 
			self.rear.next = node
			self.size = self.size + 1
		end

		self.rear = node
	end

	def isEmpty() 
		if (self.size == 0) 
			return true
		else 
			return false
		end

	end

	#  Get a front element of queue
	def peek() 
		if (self.isEmpty() == true) 
			print("\n Empty Queue \n")
			#  When stack is empty
			return -1
		else 
			return self.front.data
		end

	end

	def isSize() 
		return self.size
	end

	#  Remove a front node of a queue
	def dequeue() 
		if (self.isEmpty() == true) 
			#  When queue is empty
			return -1
		else 
			data = self.peek()
			if (self.front == self.rear) 
				#  When queue contains only one node
				self.rear = nil
				self.front = nil
			else 
				self.front = self.front.next
				self.front.prev = nil
			end

			#  Change queue size
			self.size -= 1
			return data
		end

	end

	#  Print elements of queue
	def printQdata() 
		node = self.front
		print("\n Queue Element \n")
		while (node != nil) 
			print(" ", node.data)
			node = node.next
		end

		print("\n")
	end

end

def main() 
	q = MyQueue.new()
	#  Add queue element
	q.enqueue(1)
	q.enqueue(2)
	q.enqueue(3)
	q.enqueue(4)
	q.enqueue(5)
	q.printQdata()
	print(" Size : ", q.isSize())
	#  Remove queue element
	print("\n Dequeue Node : ", q.dequeue())
	print("\n Dequeue Node : ", q.dequeue())
	print("\n Dequeue Node : ", q.dequeue())
	q.printQdata()
	print(" Size : ", q.isSize())
end

main()

Output

 Queue Element 
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element 
 4 5
 Size : 2
/*
    Scala Program 
    Implement queue using doubly linked list
*/
class QNode(var data: Int , var next: QNode , var prev: QNode)
{
	def this(data: Int, prev: QNode)
	{
		this(data, null, prev);
	}
}
class MyQueue(var front: QNode , var rear: QNode , var size: Int)
{
	def this()
	{
		this(null, null, 0);
	}
	// Add a node into queue
	def enqueue(data: Int): Unit = {
		//Create a dynamic node
		var node: QNode = new QNode(data, this.rear);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.size = 1;
		}
		else
		{
			this.rear.next = node;
			this.size = this.size + 1;
		}
		this.rear = node;
	}
	def isEmpty(): Boolean = {
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	def peek(): Int = {
		if (this.isEmpty() == true)
		{
			print("\n Empty Queue \n");
			// When stack is empty
			return -1;
		}
		else
		{
			return this.front.data;
		}
	}
	def isSize(): Int = {
		return this.size;
	}
	// Remove a front node of a queue
	def dequeue(): Int = {
		if (this.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			var data: Int = this.peek();
			if (this.front == this.rear)
			{
				// When queue contains only one node
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front.next;
				this.front.prev = null;
			}
			// Change queue size
			this.size -= 1;
			return data;
		}
	}
	// Print elements of queue
	def printQdata(): Unit = {
		var node: QNode = this.front;
		print("\n Queue Element \n");
		while (node != null)
		{
			print(" " + node.data);
			node = node.next;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var q: MyQueue = new MyQueue();
		// Add queue element
		q.enqueue(1);
		q.enqueue(2);
		q.enqueue(3);
		q.enqueue(4);
		q.enqueue(5);
		q.printQdata();
		print(" Size : " + q.isSize());
		// Remove queue element
		print("\n Dequeue Node : " + q.dequeue());
		print("\n Dequeue Node : " + q.dequeue());
		print("\n Dequeue Node : " + q.dequeue());
		q.printQdata();
		print(" Size : " + q.isSize());
	}
}

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2
/*
    Swift 4 Program 
    Implement queue using doubly linked list
*/
class QNode
{
	var data: Int;
	var next: QNode? ;
	var prev: QNode? ;
	init(_ data: Int, _ prev: QNode? )
	{
		self.data = data;
		self.next = nil;
		self.prev = prev;
	}
}
class MyQueue
{
	var front: QNode? ;
	var rear: QNode? ;
	var size: Int;
	init()
	{
		self.front = nil;
		self.rear = nil;
		self.size = 0;
	}
	// Add a node into queue
	func enqueue(_ data: Int)
	{
		//Create a dynamic node
		let node: QNode? = QNode(data, self.rear);
		if (self.front == nil)
		{
			// When adding a first node of queue
			self.front = node;
			self.size = 1;
		}
		else
		{
			self.rear!.next = node;
			self.size = self.size + 1;
		}
		self.rear = node;
	}
	func isEmpty()->Bool
	{
		if (self.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	func peek()->Int
	{
		if (self.isEmpty() == true)
		{
			print("\n Empty Queue ");
			// When stack is empty
			return -1;
		}
		else
		{
			return self.front!.data;
		}
	}
	func isSize()->Int
	{
		return self.size;
	}
	// Remove a front node of a queue
	func dequeue()->Int
	{
		if (self.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			let data: Int = self.peek();
			if (self.front === self.rear)
			{
				// When queue contains only one node
				self.rear = nil;
				self.front = nil;
			}
			else
			{
				self.front = self.front!.next;
				self.front!.prev = nil;
			}
			// Change queue size
			self.size -= 1;
			return data;
		}
	}
	// Print elements of queue
	func printQdata()
	{
		var node: QNode? = self.front;
		print("\n Queue Element ");
		while (node  != nil)
		{
			print(" ", node!.data, terminator: "");
			node = node!.next;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let q: MyQueue = MyQueue();
	// Add queue element
	q.enqueue(1);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(4);
	q.enqueue(5);
	q.printQdata();
	print(" Size : ", q.isSize(), terminator: "");
	// Remove queue element
	print("\n Dequeue Node : ", q.dequeue(), terminator: "");
	print("\n Dequeue Node : ", q.dequeue(), terminator: "");
	print("\n Dequeue Node : ", q.dequeue(), terminator: "");
	q.printQdata();
	print(" Size : ", q.isSize(), terminator: "");
}
main();

Output

 Queue Element
  1  2  3  4  5
 Size :  5
 Dequeue Node :  1
 Dequeue Node :  2
 Dequeue Node :  3
 Queue Element
  4  5
 Size :  2
/*
    Kotlin Program 
    Implement queue using doubly linked list
*/
class QNode
{
	var data: Int;
	var next: QNode? ;
	var prev: QNode? ;
	constructor(data: Int, prev: QNode? )
	{
		this.data = data;
		this.next = null;
		this.prev = prev;
	}
}
class MyQueue
{
	var front: QNode ? ;
	var rear: QNode ? ;
	var size: Int;
	constructor()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a node into queue
	fun enqueue(data: Int): Unit
	{
		//Create a dynamic node
		var node: QNode ? = QNode(data, this.rear);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.size = 1;
		}
		else
		{
			this.rear?.next = node;
			this.size = this.size + 1;
		}
		this.rear = node;
	}
	fun isEmpty(): Boolean
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	fun peek(): Int
	{
		if (this.isEmpty() == true)
		{
			print("\n Empty Queue \n");
			// When stack is empty
			return -1;
		}
		else
		{
			return this.front!!.data;
		}
	}
	fun isSize(): Int
	{
		return this.size;
	}
	// Remove a front node of a queue
	fun dequeue(): Int
	{
		if (this.isEmpty() == true)
		{
			// When queue is empty
			return -1;
		}
		else
		{
			var data: Int = this.peek();
			if (this.front == this.rear)
			{
				// When queue contains only one node
				this.rear = null;
				this.front = null;
			}
			else
			{
				this.front = this.front?.next;
				this.front?.prev = null;
			}
			// Change queue size
			this.size -= 1;
			return data;
		}
	}
	// Print elements of queue
	fun printQdata(): Unit
	{
		var node: QNode ? = this.front;
		print("\n Queue Element \n");
		while (node != null)
		{
			print(" " + node.data);
			node = node.next;
		}
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var q: MyQueue = MyQueue();
	// Add queue element
	q.enqueue(1);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(4);
	q.enqueue(5);
	q.printQdata();
	print(" Size : " + q.isSize());
	// Remove queue element
	print("\n Dequeue Node : " + q.dequeue());
	print("\n Dequeue Node : " + q.dequeue());
	print("\n Dequeue Node : " + q.dequeue());
	q.printQdata();
	print(" Size : " + q.isSize());
}

Output

 Queue Element
 1 2 3 4 5
 Size : 5
 Dequeue Node : 1
 Dequeue Node : 2
 Dequeue Node : 3
 Queue Element
 4 5
 Size : 2

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