Implement priority queue with pair

Here given code implementation process.

/*
    C Program 
    Implement priority queue with pair
*/
#include <stdio.h>
#include <stdlib.h>

// Create structure of Doubly Linked List Node
struct QNode
{
    int first;
    int second;
    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 first,int second)
{

    //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->first = first;
        node->second = second;
        node->next = NULL;
        node->prev = NULL;
        if (q->front == NULL)
        {
            // When adding a first node of queue
            q->front = node;
            q->rear = node;
        }
        else if (q->front->first <= first)
        {
            // Add node at beginning position
            node->next = q->front;
            q->front->prev = node;
            q->front = node;
        }
        else if (q->rear->first >= first)
        {
            // Add node at last position
            node->prev = q->rear;
            q->rear->next = node;
            q->rear = node;
        }
        else
        {
            struct QNode *temp = q->front;
            // Find the location of inserting priority node
            while (temp->first > first)
            {
                temp = temp->next;
            }
            // Add node
            node->next = temp;
            node->prev = temp->prev;
            temp->prev = node;
            if (node->prev != NULL)
            {
                node->prev->next = node;
            }
        }
        q->size = q->size + 1;
    }
}
int isEmpty(struct MyQueue *q)
{
    if (q->size == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
// Get a front element of queue
struct QNode* peek(struct MyQueue *q)
{
    if (isEmpty(q) == 1)
    {
        // When stack is empty
        return NULL;
    }
    else
    {
        return q->front;
    }
}
int isSize(struct MyQueue *q)
{
    return q->size;
}
// Remove a front node of a queue
void deQueue(struct MyQueue *q)
{
    if (isEmpty(q) == 0)
    {
    
        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--;

        printf("\n Dequeue Pair (%d  %d) ",temp->first,temp->second);
        free(temp);
       
    }
    else
    {
        printf("\n Empty Queue \n");
    }
}
// Print elements of queue
void printQdata(struct MyQueue *q)
{
    struct QNode *node = q->front;
    printf("\n Queue Element ");
    while (node != NULL)
    {
        printf("\n %d  %d", node->first,node->second);
        node = node->next;
    }
    printf("\n");
}
int main(int argc, char
    const *argv[])
{
    struct MyQueue *q = newMyQueue();
    // Add queue element
    enQueue(q, 4,5);
    enQueue(q, 2,8);
    enQueue(q, 3,1);
    enQueue(q, 9,2);
    enQueue(q, 5,7);
    printQdata(q);
    printf(" Size : %d", isSize(q));

    // Remove queue element
    deQueue(q);
    deQueue(q);
    deQueue(q);

    printQdata(q);
    printf(" Size : %d", isSize(q));
    struct QNode * element = peek(q);
    if(element != NULL)
    {
        printf("\n Get Element : %d  %d \n",element->first,element->second);
    }
    return 0;
}

Output

 Queue Element
 9  2
 5  7
 4  5
 3  1
 2  8
 Size : 5
 Dequeue Pair (9  2)
 Dequeue Pair (5  7)
 Dequeue Pair (4  5)
 Queue Element
 3  1
 2  8
 Size : 2
 Get Element : 3  1
/*
    Java Program 
    Implement priority queue with pair
*/
class QNode
{
	public int first;
  	public int second;
	public QNode next;
	public QNode prev;
	public QNode(int first, int second)
	{
		this.first = first;
      	this.second = second;
		this.next = null;
		this.prev = null;
	}
}
public class PriorityQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public PriorityQueue()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a node into queue Priority queue
	public void enQueue(int first, int second)
	{
		//Create a dynamic node
		QNode node = new QNode(first, second);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.rear = node;
		}
		else if (this.front.first <= first)
		{
			// Add node at beginning position
			node.next = this.front;
			this.front.prev = node;
			this.front = node;
		}
		else if (this.rear.first >= first)
		{
			// Add node at last position
			node.prev = this.rear;
			this.rear.next = node;
			this.rear = node;
		}
		else
		{
			QNode temp = this.front;
			// Find the location of inserting priority node
			while (temp.first > first)
			{
				temp = temp.next;
			}
			// Add node
			node.next = temp;
			node.prev = temp.prev;
			temp.prev = node;
			if (node.prev != null)
			{
				node.prev.next = node;
			}
		}
		this.size = this.size + 1;
	}
	public boolean isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	public QNode peek()
	{
		if (this.isEmpty() == true)
		{
			System.out.print("\n Empty Queue \n");
			// When stack is empty
			return null;
		}
		else
		{
			return this.front;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	public void deQueue()
	{
		if (this.isEmpty() == false)
		{
			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;
			}
           	System.out.print("\n Dequeue Pair "+temp.first+" "+temp.second);
			// Change queue size
			this.size--;
		}
	}
	// Print elements of queue
	public void printQdata()
	{
		QNode node = this.front;
		System.out.print("\n Queue Element ");
		while (node != null)
		{
			System.out.print("\n " + node.first + " " + node.second);
			node = node.next;
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		PriorityQueue q = new PriorityQueue();
		// Add queue pair 
		q.enQueue(4, 5);
		q.enQueue(2, 8);
		q.enQueue(3, 1);
		q.enQueue(9, 2);
		q.enQueue(5, 7);
		q.printQdata();
		System.out.print(" Size : " + q.isSize());
		// Remove queue element
		q.deQueue();
		q.deQueue();
		q.deQueue();
		q.printQdata();
		System.out.print(" Size : " + q.isSize());
		QNode element = q.peek();
		if (element != null)
		{
			System.out.print("\n Get Element : " + element.first + " " + element.second + " \n");
		}
	}
}

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program 
    Implement priority queue with pair
*/
class QNode
{
	public: int first;
	int second;
	QNode *next;
	QNode *prev;
	QNode(int first, int second)
	{
		this->first = first;
		this->second = second;
		this->next = NULL;
		this->prev = NULL;
	}
};
class PriorityQueue
{
	public: QNode *front;
	QNode *rear;
	int size;
	PriorityQueue()
	{
		this->front = NULL;
		this->rear = NULL;
		this->size = 0;
	}
	// Add a node into queue Priority queue
	void enQueue(int first, int second)
	{
		//Create a dynamic node
		QNode *node = new QNode(first, second);
		if (this->front == NULL)
		{
			// When adding a first node of queue
			this->front = node;
			this->rear = node;
		}
		else if (this->front->first <= first)
		{
			// Add node at beginning position
			node->next = this->front;
			this->front->prev = node;
			this->front = node;
		}
		else if (this->rear->first >= first)
		{
			// Add node at last position
			node->prev = this->rear;
			this->rear->next = node;
			this->rear = node;
		}
		else
		{
			QNode *temp = this->front;
			// Find the location of inserting priority node
			while (temp->first > first)
			{
				temp = temp->next;
			}
			// Add node
			node->next = temp;
			node->prev = temp->prev;
			temp->prev = node;
			if (node->prev != NULL)
			{
				node->prev->next = node;
			}
		}
		this->size = this->size + 1;
	}
	bool isEmpty()
	{
		if (this->size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	QNode *peek()
	{
		if (this->isEmpty() == true)
		{
			// When stack is empty
			cout << "\n Empty Queue \n";
			return NULL;
		}
		else
		{
			return this->front;
		}
	}
	int isSize()
	{
		return this->size;
	}
	// Remove a front node of a queue
	void deQueue()
	{
		if (this->isEmpty() == false)
		{
			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;
			}
			cout << "\n Dequeue Pair " << temp->first << " " << temp->second;
			// Change queue size
			this->size--;
		}
	}
	// Print elements of queue
	void printQdata()
	{
		QNode *node = this->front;
		cout << "\n Queue Element ";
		while (node != NULL)
		{
			cout << "\n " << node->first << " " << node->second;
			node = node->next;
		}
		cout << "\n";
	}
};
int main()
{
	PriorityQueue q = PriorityQueue();
	// Add queue pair
	q.enQueue(4, 5);
	q.enQueue(2, 8);
	q.enQueue(3, 1);
	q.enQueue(9, 2);
	q.enQueue(5, 7);
	q.printQdata();
	cout << " Size : " << q.isSize();
	// Remove queue element
	q.deQueue();
	q.deQueue();
	q.deQueue();
	q.printQdata();
	cout << " Size : " << q.isSize();
	QNode *element = q.peek();
	if (element != NULL)
	{
		cout << "\n Get Element : " << element->first << " " << element->second << " \n";
	}
	return 0;
}

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
// Include namespace system
using System;
/*
    C# Program 
    Implement priority queue with pair
*/
public class QNode
{
	public int first;
	public int second;
	public QNode next;
	public QNode prev;
	public QNode(int first, int second)
	{
		this.first = first;
		this.second = second;
		this.next = null;
		this.prev = null;
	}
}
public class PriorityQueue
{
	public QNode front;
	public QNode rear;
	public int size;
	public PriorityQueue()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a node into queue Priority queue
	public void enQueue(int first, int second)
	{
		//Create a dynamic node
		QNode node = new QNode(first, second);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.rear = node;
		}
		else if (this.front.first <= first)
		{
			// Add node at beginning position
			node.next = this.front;
			this.front.prev = node;
			this.front = node;
		}
		else if (this.rear.first >= first)
		{
			// Add node at last position
			node.prev = this.rear;
			this.rear.next = node;
			this.rear = node;
		}
		else
		{
			QNode temp = this.front;
			// Find the location of inserting priority node
			while (temp.first > first)
			{
				temp = temp.next;
			}
			// Add node
			node.next = temp;
			node.prev = temp.prev;
			temp.prev = node;
			if (node.prev != null)
			{
				node.prev.next = node;
			}
		}
		this.size = this.size + 1;
	}
	public Boolean isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	public QNode peek()
	{
		if (this.isEmpty() == true)
		{
			// When stack is empty
			Console.Write("\n Empty Queue \n");
			return null;
		}
		else
		{
			return this.front;
		}
	}
	public int isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	public void deQueue()
	{
		if (this.isEmpty() == false)
		{
			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;
			}
			Console.Write("\n Dequeue Pair " + temp.first + " " + temp.second);
			// Change queue size
			this.size--;
		}
	}
	// Print elements of queue
	public void printQdata()
	{
		QNode node = this.front;
		Console.Write("\n Queue Element ");
		while (node != null)
		{
			Console.Write("\n " + node.first + " " + node.second);
			node = node.next;
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		PriorityQueue q = new PriorityQueue();
		// Add queue pair
		q.enQueue(4, 5);
		q.enQueue(2, 8);
		q.enQueue(3, 1);
		q.enQueue(9, 2);
		q.enQueue(5, 7);
		q.printQdata();
		Console.Write(" Size : " + q.isSize());
		// Remove queue element
		q.deQueue();
		q.deQueue();
		q.deQueue();
		q.printQdata();
		Console.Write(" Size : " + q.isSize());
		QNode element = q.peek();
		if (element != null)
		{
			Console.Write("\n Get Element : " + element.first + " " + element.second + " \n");
		}
	}
}

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
<?php
/*
    Php Program 
    Implement priority queue with pair
*/
class QNode
{
	public $first;
	public $second;
	public $next;
	public $prev;

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

	function __construct()
	{
		$this->front = null;
		$this->rear = null;
		$this->size = 0;
	}
	// Add a node into queue Priority queue
	public	function enQueue($first, $second)
	{
		//Create a dynamic node
		$node = new QNode($first, $second);
		if ($this->front == null)
		{
			// When adding a first node of queue
			$this->front = $node;
			$this->rear = $node;
		}
		else if ($this->front->first <= $first)
		{
			// Add node at beginning position
			$node->next = $this->front;
			$this->front->prev = $node;
			$this->front = $node;
		}
		else if ($this->rear->first >= $first)
		{
			// Add node at last position
			$node->prev = $this->rear;
			$this->rear->next = $node;
			$this->rear = $node;
		}
		else
		{
			$temp = $this->front;
			// Find the location of inserting priority node
			while ($temp->first > $first)
			{
				$temp = $temp->next;
			}
			// Add node
			$node->next = $temp;
			$node->prev = $temp->prev;
			$temp->prev = $node;
			if ($node->prev != null)
			{
				$node->prev->next = $node;
			}
		}
		$this->size = $this->size + 1;
	}
	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)
		{
			// When stack is empty
			echo "\n Empty Queue \n";
			return null;
		}
		else
		{
			return $this->front;
		}
	}
	public	function isSize()
	{
		return $this->size;
	}
	// Remove a front node of a queue
	public	function deQueue()
	{
		if ($this->isEmpty() == false)
		{
			$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;
			}
			echo "\n Dequeue Pair ". $temp->first ." ". $temp->second;
			// Change queue size
			$this->size--;
		}
	}
	// Print elements of queue
	public	function printQdata()
	{
		$node = $this->front;
		echo "\n Queue Element ";
		while ($node != null)
		{
			echo "\n ". $node->first ." ". $node->second;
			$node = $node->next;
		}
		echo "\n";
	}
}

function main()
{
	$q = new PriorityQueue();
	// Add queue pair
	$q->enQueue(4, 5);
	$q->enQueue(2, 8);
	$q->enQueue(3, 1);
	$q->enQueue(9, 2);
	$q->enQueue(5, 7);
	$q->printQdata();
	echo " Size : ". $q->isSize();
	// Remove queue element
	$q->deQueue();
	$q->deQueue();
	$q->deQueue();
	$q->printQdata();
	echo " Size : ". $q->isSize();
	$element = $q->peek();
	if ($element != null)
	{
		echo "\n Get Element : ". $element->first ." ". $element->second ." \n";
	}
}
main();

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
/*
    Node Js Program 
    Implement priority queue with pair
*/
class QNode
{
	constructor(first, second)
	{
		this.first = first;
		this.second = second;
		this.next = null;
		this.prev = null;
	}
}
class PriorityQueue
{
	constructor()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a node into queue Priority queue
	enQueue(first, second)
	{
		//Create a dynamic node
		var node = new QNode(first, second);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.rear = node;
		}
		else if (this.front.first <= first)
		{
			// Add node at beginning position
			node.next = this.front;
			this.front.prev = node;
			this.front = node;
		}
		else if (this.rear.first >= first)
		{
			// Add node at last position
			node.prev = this.rear;
			this.rear.next = node;
			this.rear = node;
		}
		else
		{
			var temp = this.front;
			// Find the location of inserting priority node
			while (temp.first > first)
			{
				temp = temp.next;
			}
			// Add node
			node.next = temp;
			node.prev = temp.prev;
			temp.prev = node;
			if (node.prev != null)
			{
				node.prev.next = node;
			}
		}
		this.size = this.size + 1;
	}
	isEmpty()
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	peek()
	{
		if (this.isEmpty() == true)
		{
			// When stack is empty
			process.stdout.write("\n Empty Queue \n");
			return null;
		}
		else
		{
			return this.front;
		}
	}
	isSize()
	{
		return this.size;
	}
	// Remove a front node of a queue
	deQueue()
	{
		if (this.isEmpty() == false)
		{
			var 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;
			}
			process.stdout.write("\n Dequeue Pair " + temp.first + " " + temp.second);
			// Change queue size
			this.size--;
		}
	}
	// Print elements of queue
	printQdata()
	{
		var node = this.front;
		process.stdout.write("\n Queue Element ");
		while (node != null)
		{
			process.stdout.write("\n " + node.first + " " + node.second);
			node = node.next;
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var q = new PriorityQueue();
	// Add queue pair
	q.enQueue(4, 5);
	q.enQueue(2, 8);
	q.enQueue(3, 1);
	q.enQueue(9, 2);
	q.enQueue(5, 7);
	q.printQdata();
	process.stdout.write(" Size : " + q.isSize());
	// Remove queue element
	q.deQueue();
	q.deQueue();
	q.deQueue();
	q.printQdata();
	process.stdout.write(" Size : " + q.isSize());
	var element = q.peek();
	if (element != null)
	{
		process.stdout.write("\n Get Element : " + element.first + " " + element.second + " \n");
	}
}
main();

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
#  Python 3 Program 
#  Implement priority queue with pair

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

class PriorityQueue :
	
	def __init__(self) :
		self.front = None
		self.rear = None
		self.size = 0
	
	#  Add a node into queue Priority queue
	def enQueue(self, first, second) :
		# Create a dynamic node
		node = QNode(first, second)
		if (self.front == None) :
			#  When adding a first node of queue
			self.front = node
			self.rear = node
		
		elif(self.front.first <= first) :
			#  Add node at beginning position
			node.next = self.front
			self.front.prev = node
			self.front = node
		
		elif(self.rear.first >= first) :
			#  Add node at last position
			node.prev = self.rear
			self.rear.next = node
			self.rear = node
		else :
			temp = self.front
			#  Find the location of inserting priority node
			while (temp.first > first) :
				temp = temp.next
			
			#  Add node
			node.next = temp
			node.prev = temp.prev
			temp.prev = node
			if (node.prev != None) :
				node.prev.next = node
			
		
		self.size = self.size + 1
	
	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 None
		else :
			return self.front
		
	
	def isSize(self) :
		return self.size
	
	#  Remove a front node of a queue
	def deQueue(self) :
		if (self.isEmpty() == False) :
			temp = self.front
			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
			
			print("\n Dequeue Pair ", temp.first ," ", temp.second, end = "")
			#  Change queue size
			self.size -= 1
		
	
	#  Print elements of queue
	def printQdata(self) :
		node = self.front
		print("\n Queue Element ", end = "")
		while (node != None) :
			print("\n ", node.first ," ", node.second, end = "")
			node = node.next
		
		print(end = "\n")
	

def main() :
	q = PriorityQueue()
	#  Add queue pair 
	q.enQueue(4, 5)
	q.enQueue(2, 8)
	q.enQueue(3, 1)
	q.enQueue(9, 2)
	q.enQueue(5, 7)
	q.printQdata()
	print(" Size : ", q.isSize(), end = "")
	#  Remove queue element
	q.deQueue()
	q.deQueue()
	q.deQueue()
	q.printQdata()
	print(" Size : ", q.isSize(), end = "")
	element = q.peek()
	if (element != None) :
		print("\n Get Element : ", element.first ," ", element.second ," ")
	

if __name__ == "__main__": main()

Output

 Queue Element
  9   2
  5   7
  4   5
  3   1
  2   8
 Size :  5
 Dequeue Pair  9   2
 Dequeue Pair  5   7
 Dequeue Pair  4   5
 Queue Element
  3   1
  2   8
 Size :  2
 Get Element :  3   1
#  Ruby Program 
#  Implement priority queue with pair

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

end

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

	#  Add a node into queue Priority queue
	def enQueue(first, second) 
		# Create a dynamic node
		node = QNode.new(first, second)
		if (self.front == nil) 
			#  When adding a first node of queue
			self.front = node
			self.rear = node
		elsif(self.front.first <= first) 
			#  Add node at beginning position
			node.next = self.front
			self.front.prev = node
			self.front = node
		elsif(self.rear.first >= first) 
			#  Add node at last position
			node.prev = self.rear
			self.rear.next = node
			self.rear = node
		else 
			temp = self.front
			#  Find the location of inserting priority node
			while (temp.first > first) 
				temp = temp.next
			end

			#  Add node
			node.next = temp
			node.prev = temp.prev
			temp.prev = node
			if (node.prev != nil) 
				node.prev.next = node
			end

		end

		self.size = self.size + 1
	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 nil
		else 
			return self.front
		end

	end

	def isSize() 
		return self.size
	end

	#  Remove a front node of a queue
	def deQueue() 
		if (self.isEmpty() == false) 
			temp = self.front
			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

			print("\n Dequeue Pair ", temp.first ," ", temp.second)
			#  Change queue size
			self.size -= 1
		end

	end

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

		print("\n")
	end

end

def main() 
	q = PriorityQueue.new()
	#  Add queue pair 
	q.enQueue(4, 5)
	q.enQueue(2, 8)
	q.enQueue(3, 1)
	q.enQueue(9, 2)
	q.enQueue(5, 7)
	q.printQdata()
	print(" Size : ", q.isSize())
	#  Remove queue element
	q.deQueue()
	q.deQueue()
	q.deQueue()
	q.printQdata()
	print(" Size : ", q.isSize())
	element = q.peek()
	if (element != nil) 
		print("\n Get Element : ", element.first ," ", element.second ," \n")
	end

end

main()

Output

 Queue Element 
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element 
 3 1
 2 8
 Size : 2
 Get Element : 3 1 
/*
    Scala Program 
    Implement priority queue with pair
*/
class QNode(var first: Int , var second: Int , var next: QNode , var prev: QNode)
{
	def this(first: Int, second: Int)
	{
		this(first, second, null, null);
	}
}
class PriorityQueue(var front: QNode , var rear: QNode , var size: Int)
{
	def this()
	{
		this(null, null, 0);
	}
	// Add a node into queue Priority queue
	def enQueue(first: Int, second: Int): Unit = {
		//Create a dynamic node
		var node: QNode = new QNode(first, second);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.rear = node;
		}
		else if (this.front.first <= first)
		{
			// Add node at beginning position
			node.next = this.front;
			this.front.prev = node;
			this.front = node;
		}
		else if (this.rear.first >= first)
		{
			// Add node at last position
			node.prev = this.rear;
			this.rear.next = node;
			this.rear = node;
		}
		else
		{
			var temp: QNode = this.front;
			// Find the location of inserting priority node
			while (temp.first > first)
			{
				temp = temp.next;
			}
			// Add node
			node.next = temp;
			node.prev = temp.prev;
			temp.prev = node;
			if (node.prev != null)
			{
				node.prev.next = node;
			}
		}
		this.size = this.size + 1;
	}
	def isEmpty(): Boolean = {
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	def peek(): QNode = {
		if (this.isEmpty() == true)
		{
			// When stack is empty
			print("\n Empty Queue \n");
			return null;
		}
		else
		{
			return this.front;
		}
	}
	def isSize(): Int = {
		return this.size;
	}
	// Remove a front node of a queue
	def deQueue(): Unit = {
		if (this.isEmpty() == false)
		{
			var temp: QNode = 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;
			}
			print("\n Dequeue Pair " + temp.first + " " + temp.second);
			// Change queue size
			this.size -= 1;
		}
	}
	// Print elements of queue
	def printQdata(): Unit = {
		var node: QNode = this.front;
		print("\n Queue Element ");
		while (node != null)
		{
			print("\n " + node.first + " " + node.second);
			node = node.next;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var q: PriorityQueue = new PriorityQueue();
		// Add queue pair
		q.enQueue(4, 5);
		q.enQueue(2, 8);
		q.enQueue(3, 1);
		q.enQueue(9, 2);
		q.enQueue(5, 7);
		q.printQdata();
		print(" Size : " + q.isSize());
		// Remove queue element
		q.deQueue();
		q.deQueue();
		q.deQueue();
		q.printQdata();
		print(" Size : " + q.isSize());
		var element: QNode = q.peek();
		if (element != null)
		{
			print("\n Get Element : " + element.first + " " + element.second + " \n");
		}
	}
}

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 1
/*
    Swift 4 Program 
    Implement priority queue with pair
*/
class QNode
{
	var first: Int;
	var second: Int;
	var next: QNode? ;
	var prev: QNode? ;
	init(_ first: Int, _ second: Int)
	{
		self.first = first;
		self.second = second;
		self.next = nil;
		self.prev = nil;
	}
}
class PriorityQueue
{
	var front: QNode? ;
	var rear: QNode? ;
	var size: Int;
	init()
	{
		self.front = nil;
		self.rear = nil;
		self.size = 0;
	}
	// Add a node into queue Priority queue
	func enQueue(_ first: Int, _ second: Int)
	{
		//Create a dynamic node
		let node: QNode? = QNode(first, second);
		if (self.front == nil)
		{
			// When adding a first node of queue
			self.front = node;
			self.rear = node;
		}
		else if (self.front!.first <= first)
		{
			// Add node at beginning position
			node!.next = self.front;
			self.front!.prev = node;
			self.front = node;
		}
		else if (self.rear!.first >= first)
		{
			// Add node at last position
			node!.prev = self.rear;
			self.rear!.next = node;
			self.rear = node;
		}
		else
		{
			var temp: QNode? = self.front;
			// Find the location of inserting priority node
			while (temp!.first > first)
			{
				temp = temp!.next;
			}
			// Add node
			node!.next = temp;
			node!.prev = temp!.prev;
			temp!.prev = node;
			if (node!.prev  != nil)
			{
				node!.prev!.next = node;
			}
		}
		self.size = self.size + 1;
	}
	func isEmpty()->Bool
	{
		if (self.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	func peek()->QNode?
	{
		if (self.isEmpty() == true)
		{
			// When stack is empty
			print("\n Empty Queue ");
			return nil;
		}
		else
		{
			return self.front;
		}
	}
	func isSize()->Int
	{
		return self.size;
	}
	// Remove a front node of a queue
	func deQueue()
	{
		if (self.isEmpty() == false)
		{
			let temp: QNode? = self.front;
			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;
			}
			print("\n Dequeue Pair ", temp!.first ," ", temp!.second, terminator: "");
			// Change queue size
			self.size -= 1;
		}
	}
	// Print elements of queue
	func printQdata()
	{
		var node: QNode? = self.front;
		print("\n Queue Element ", terminator: "");
		while (node  != nil)
		{
			print("\n ", node!.first ," ", node!.second, terminator: "");
			node = node!.next;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let q: PriorityQueue = PriorityQueue();
	// Add queue pair
	q.enQueue(4, 5);
	q.enQueue(2, 8);
	q.enQueue(3, 1);
	q.enQueue(9, 2);
	q.enQueue(5, 7);
	q.printQdata();
	print(" Size : ", q.isSize(), terminator: "");
	// Remove queue element
	q.deQueue();
	q.deQueue();
	q.deQueue();
	q.printQdata();
	print(" Size : ", q.isSize(), terminator: "");
	let element: QNode? = q.peek();
	if (element  != nil)
	{
		print("\n Get Element : ", element!.first ," ", element!.second ," ");
	}
}
main();

Output

 Queue Element
  9   2
  5   7
  4   5
  3   1
  2   8
 Size :  5
 Dequeue Pair  9   2
 Dequeue Pair  5   7
 Dequeue Pair  4   5
 Queue Element
  3   1
  2   8
 Size :  2
 Get Element :  3   1
/*
    Kotlin Program 
    Implement priority queue with pair
*/
class QNode
{
	var first: Int;
	var second: Int;
	var next: QNode ? ;
	var prev: QNode ? ;
	constructor(first: Int, second: Int)
	{
		this.first = first;
		this.second = second;
		this.next = null;
		this.prev = null;
	}
}
class PriorityQueue
{
	var front: QNode ? ;
	var rear: QNode ? ;
	var size: Int;
	constructor()
	{
		this.front = null;
		this.rear = null;
		this.size = 0;
	}
	// Add a node into queue Priority queue
	fun enQueue(first: Int, second: Int): Unit
	{
		//Create a dynamic node
		var node: QNode ? = QNode(first, second);
		if (this.front == null)
		{
			// When adding a first node of queue
			this.front = node;
			this.rear = node;
		}
		else if (this.front!!.first <= first)
		{
			// Add node at beginning position
			node?.next = this.front;
			this.front?.prev = node;
			this.front = node;
		}
		else if (this.rear!!.first >= first)
		{
			// Add node at last position
			node?.prev = this.rear;
			this.rear?.next = node;
			this.rear = node;
		}
		else
		{
			var temp: QNode ? = this.front;
			// Find the location of inserting priority node
			while (temp != null && temp.first > first)
			{
				temp = temp.next;
			}
			// Add node
			node?.next = temp;
			node?.prev = temp?.prev;
			temp?.prev = node;
			if (node?.prev != null)
			{
				node.prev?.next = node;
			}
		}
		this.size = this.size + 1;
	}
	fun isEmpty(): Boolean
	{
		if (this.size == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// Get a front element of queue
	fun peek(): QNode ?
	{
		if (this.isEmpty() == true)
		{
			// When stack is empty
			print("\n Empty Queue \n");
			return null;
		}
		else
		{
			return this.front;
		}
	}
	fun isSize(): Int
	{
		return this.size;
	}
	// Remove a front node of a queue
	fun deQueue(): Unit
	{
		if (this.isEmpty() == false)
		{
			var temp: QNode? = 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;
			}
			print("\n Dequeue Pair " + temp!!.first + " " + temp.second);
			// Change queue size
			this.size -= 1;
		}
	}
	// Print elements of queue
	fun printQdata(): Unit
	{
		var node: QNode ? = this.front;
		print("\n Queue Element ");
		while (node != null)
		{
			print("\n " + node.first + " " + node.second);
			node = node.next;
		}
		print("\n");
	}
}
fun main(args: Array<String> ): Unit
{
	var q: PriorityQueue = PriorityQueue();
	// Add queue pair
	q.enQueue(4, 5);
	q.enQueue(2, 8);
	q.enQueue(3, 1);
	q.enQueue(9, 2);
	q.enQueue(5, 7);
	q.printQdata();
	print(" Size : " + q.isSize());
	// Remove queue element
	q.deQueue();
	q.deQueue();
	q.deQueue();
	q.printQdata();
	print(" Size : " + q.isSize());
	var element: QNode ? = q.peek();
	if (element != null)
	{
		print("\n Get Element : " + element.first + " " + element.second + " \n");
	}
}

Output

 Queue Element
 9 2
 5 7
 4 5
 3 1
 2 8
 Size : 5
 Dequeue Pair 9 2
 Dequeue Pair 5 7
 Dequeue Pair 4 5
 Queue Element
 3 1
 2 8
 Size : 2
 Get Element : 3 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







© 2021, kalkicode.com, All rights reserved