Efficiently implement n queues in a single array

Here given code implementation process.

``````/*
C Program
Efficiently implement n queues in a single array
*/
#include <stdio.h>

#include <stdlib.h>

// Structure of queue
struct NQueue
{
int element;
int part;
int *data;
int *front;
int *tail;
int *next;
int counter;
};
struct NQueue *makeQueue(int element, int n)
{
if (n <= 0 || element == 0)
{
return NULL;
}
// Create dynamic NQueue
struct NQueue *q = (struct NQueue *) malloc(sizeof(struct NQueue));
if (q == NULL)
{
printf("\n Memory Overflow, when creating a new Queue\n");
}
else
{
// Create memory of queue elements
q->data = (int *) malloc(element *sizeof(int));
q->next = (int *) malloc(element *sizeof(int));
q->front = (int *) malloc(n *sizeof(int));
q->tail = (int *) malloc(n *sizeof(int));
q->counter = 0;
q->element = element;
q->part = n;
int i = 0;
// Set the initial value of front and tail of queue
for (i = 0; i < n; ++i)
{
q->front[i] = -1;
q->tail[i] = -1;
}
// Set next and data value
for (i = 0; i < element - 1; ++i)
{
q->data[i] = 0;
q->next[i] = i + 1;
}
q->next[element - 1] = -1;
q->data[element - 1] = 0;
}
return q;
}
// Determine that given queue is empty or not
int isEmpty(struct NQueue *q, int selection)
{
return q->front[selection] == -1;
}
// Determine that given queue is full or not
int isFull(struct NQueue *q)
{
if (q->counter == -1)
{
return 1;
}
return 0;
}
// Handles the request of adding a new element into select queue
void enqueue(struct NQueue *q, int selection, int data)
{
if (selection < 0 || selection >= q->part)
{
// When given queue is out of range
return;
}
if (isFull(q) == 1)
{
printf("\n Queue is Full \n");
}
else
{
int location = q->next[q->counter];
if (isEmpty(q, selection) == 1)
{
// First element of queue
q->front[selection] = q->counter;
q->tail[selection] = q->counter;
}
else
{
q->next[q->tail[selection]] = q->counter;
q->tail[selection] = q->counter;
}
q->data[q->counter] = data;
q->next[q->counter] = -1;
q->counter = location;
}
}
// Handles the request of remove selected queue element
int dequeue(struct NQueue *q, int selection)
{
if (selection < 0 || selection >= q->part)
{
// When given queue is out of range
return -1;
}
if (isEmpty(q, selection) == 1)
{
printf("\n Queue is Empty \n");
return -1;
}
else
{
// Get the removed element location
int location = q->front[selection];
// Get the removed element
int item = q->data[q->front[selection]];
q->data[q->front[selection]] = 0; // reset data
// Front is move to next element of queue
q->front[selection] = q->next[location];
// Set empty location in remove element
q->next[location] = q->counter;
// Set counter to current empty location
q->counter = location;
return item;
}
}
// Display queue elements
void printQueue(struct NQueue *q)
{
int location = 0;
for (int i = 0; i < q->part; ++i)
{
printf("\n Queue %d : ", i);
// Select Queue
location = q->front[i];
while (location != -1)
{
printf(" %d", q->data[location]);
location = q->next[location];
}
}
}
int main(int argc, char
const *argv[])
{
struct NQueue *q = makeQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
enqueue(q, 0, 5);
// Put data in queue-1
enqueue(q, 1, 10);
// Put data in queue-2
enqueue(q, 2, 20);
// Put data in queue-3
enqueue(q, 3, 30);
enqueue(q, 3, 19);
// Put data in queue-0
enqueue(q, 0, 15);
enqueue(q, 0, 25);
// Put data in queue-3
enqueue(q, 3, 60);
enqueue(q, 3, 9);
enqueue(q, 3, 3);
// Put data in queue-2
enqueue(q, 2, 8);
enqueue(q, 2, 45);
printQueue(q);
// Perform remove operation
printf("\n Remove element of Queue %d is : %d", 0, dequeue(q, 0));
printf("\n Remove element of Queue %d is : %d", 0, dequeue(q, 0));
printf("\n Remove element of Queue %d is : %d", 3, dequeue(q, 3));
printf("\n Remove element of Queue %d is : %d", 1, dequeue(q, 1));
printQueue(q);
return 0;
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````/*
Java Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
public int element;
public int part;
public int []data;
public int []front;
public int []tail;
public int []next;
public int counter;
public NQueue(int element, int part)
{
this.element = element;
this.part = part;
this.counter = 0;
// Create memory of queue elements
this.data = new int[element];
this.next = new int[element];
this.front = new int[part];
this.tail = new int[part];
int i = 0;
// Set the initial value of front and tail of queue
for (i = 0; i < part; ++i)
{
this.front[i] = -1;
this.tail[i] = -1;
}
// Set next and data value
for (i = 0; i < element - 1; ++i)
{
this.data[i] = 0;
this.next[i] = i + 1;
}
this.next[element - 1] = -1;
this.data[element - 1] = 0;
}
// Determine that given queue is empty or not
public boolean isEmpty(int selection)
{
return this.front[selection] == -1;
}
// Determine that given queue is full or not
public boolean isFull()
{
if (this.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
public void enqueue(int selection, int data)
{
if (selection < 0 || selection >= this.part)
{
// When given queue is out of range
return;
}
if (this.isFull() == true)
{
System.out.print("\n Queue is Full \n");
}
else
{
int location = this.next[this.counter];
if (this.isEmpty(selection) == true)
{
// First element of queue
this.front[selection] = this.counter;
this.tail[selection] = this.counter;
}
else
{
this.next[this.tail[selection]] = this.counter;
this.tail[selection] = this.counter;
}
this.data[this.counter] = data;
this.next[this.counter] = -1;
this.counter = location;
}
}
// Handles the request of remove selected queue element
public int dequeue(int selection)
{
if (selection < 0 || selection >= this.part)
{
// When given queue is out of range
return -1;
}
else if (this.isEmpty(selection) == true)
{
System.out.print("\n Queue is Empty \n");
}
else
{
// Get the removed element location
int location = this.front[selection];
// Get the removed element
int item = this.data[this.front[selection]];
this.data[this.front[selection]] = 0;
// reset data
// Front is move to next element of queue
this.front[selection] = this.next[location];
// Set empty location in remove element
this.next[location] = this.counter;
// Set counter to current empty location
this.counter = location;
return item;
}
return -1;
}
}
public class Implementation
{
// Display queue elements
public void printQueue(NQueue q)
{
int location = 0;
for (int i = 0; i < q.part; ++i)
{
System.out.print("\n Queue " + i + " : ");
// Select Queue
location = q.front[i];
while (location != -1)
{
System.out.print(" " + q.data[location]);
location = q.next[location];
}
}
}
public static void main(String[] args)
{
NQueue q = new NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
System.out.print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
System.out.print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
System.out.print("\n Remove element of Queue " + 3 + " is : " + q.dequeue(3));
System.out.print("\n Remove element of Queue " + 1 + " is : " + q.dequeue(1));
}
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
public: int element;
int part;
int *data;
int *front;
int *tail;
int *next;
int counter;
NQueue(int element, int part)
{
this->element = element;
this->part = part;
this->counter = 0;
// Create memory of queue elements
this->data = new int[element];
this->next = new int[element];
this->front = new int[part];
this->tail = new int[part];
int i = 0;
// Set the initial value of front and tail of queue
for (i = 0; i < part; ++i)
{
this->front[i] = -1;
this->tail[i] = -1;
}
// Set next and data value
for (i = 0; i < element - 1; ++i)
{
this->data[i] = 0;
this->next[i] = i + 1;
}
this->next[element - 1] = -1;
this->data[element - 1] = 0;
}
// Determine that given queue is empty or not
bool isEmpty(int selection)
{
return this->front[selection] == -1;
}
// Determine that given queue is full or not
bool isFull()
{
if (this->counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
void enqueue(int selection, int data)
{
// When given queue is out of range
if (selection < 0 || selection >= this->part)
{
return;
}
if (this->isFull() == true)
{
cout << "\n Queue is Full \n";
}
else
{
int location = this->next[this->counter];
if (this->isEmpty(selection) == true)
{
// First element of queue
this->front[selection] = this->counter;
this->tail[selection] = this->counter;
}
else
{
this->next[this->tail[selection]] = this->counter;
this->tail[selection] = this->counter;
}
this->data[this->counter] = data;
this->next[this->counter] = -1;
this->counter = location;
}
}
// Handles the request of remove selected queue element
int dequeue(int selection)
{
if (selection < 0 || selection >= this->part)
{
// When given queue is out of range
return -1;
}
else if (this->isEmpty(selection) == true)
{
cout << "\n Queue is Empty \n";
}
else
{
// Get the removed element location
int location = this->front[selection];
// Get the removed element
int item = this->data[this->front[selection]];
this->data[this->front[selection]] = 0;
// reset data
// Front is move to next element of queue
this->front[selection] = this->next[location];
// Set empty location in remove element
this->next[location] = this->counter;
// Set counter to current empty location
this->counter = location;
return item;
}
return -1;
}
};
class Implementation
{
public:
// Display queue elements
void printQueue(NQueue q)
{
int location = 0;
for (int i = 0; i < q.part; ++i)
{
cout << "\n Queue " << i << " : ";
// Select Queue
location = q.front[i];
while (location != -1)
{
cout << " " << q.data[location];
location = q.next[location];
}
}
}
};
int main()
{
NQueue q = NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
cout << "\n Remove element of Queue " << 0 << " is : " << q.dequeue(0);
cout << "\n Remove element of Queue " << 0 << " is : " << q.dequeue(0);
cout << "\n Remove element of Queue " << 3 << " is : " << q.dequeue(3);
cout << "\n Remove element of Queue " << 1 << " is : " << q.dequeue(1);
return 0;
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````// Include namespace system
using System;
/*
C# Program for
Efficiently implement n queues in a single array
*/
public class NQueue
{
public int element;
public int part;
public int[] data;
public int[] front;
public int[] tail;
public int[] next;
public int counter;
public NQueue(int element, int part)
{
this.element = element;
this.part = part;
this.counter = 0;
// Create memory of queue elements
this.data = new int[element];
this.next = new int[element];
this.front = new int[part];
this.tail = new int[part];
int i = 0;
// Set the initial value of front and tail of queue
for (i = 0; i < part; ++i)
{
this.front[i] = -1;
this.tail[i] = -1;
}
// Set next and data value
for (i = 0; i < element - 1; ++i)
{
this.data[i] = 0;
this.next[i] = i + 1;
}
this.next[element - 1] = -1;
this.data[element - 1] = 0;
}
// Determine that given queue is empty or not
public Boolean isEmpty(int selection)
{
return this.front[selection] == -1;
}
// Determine that given queue is full or not
public Boolean isFull()
{
if (this.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
public void enqueue(int selection, int data)
{
// When given queue is out of range
if (selection < 0 || selection >= this.part)
{
return;
}
if (this.isFull() == true)
{
Console.Write("\n Queue is Full \n");
}
else
{
int location = this.next[this.counter];
if (this.isEmpty(selection) == true)
{
// First element of queue
this.front[selection] = this.counter;
this.tail[selection] = this.counter;
}
else
{
this.next[this.tail[selection]] = this.counter;
this.tail[selection] = this.counter;
}
this.data[this.counter] = data;
this.next[this.counter] = -1;
this.counter = location;
}
}
// Handles the request of remove selected queue element
public int dequeue(int selection)
{
if (selection < 0 || selection >= this.part)
{
// When given queue is out of range
return -1;
}
else if (this.isEmpty(selection) == true)
{
Console.Write("\n Queue is Empty \n");
}
else
{
// Get the removed element location
int location = this.front[selection];
// Get the removed element
int item = this.data[this.front[selection]];
this.data[this.front[selection]] = 0;
// reset data
// Front is move to next element of queue
this.front[selection] = this.next[location];
// Set empty location in remove element
this.next[location] = this.counter;
// Set counter to current empty location
this.counter = location;
return item;
}
return -1;
}
}
public class Implementation
{
// Display queue elements
public void printQueue(NQueue q)
{
int location = 0;
for (int i = 0; i < q.part; ++i)
{
Console.Write("\n Queue " + i + " : ");
// Select Queue
location = q.front[i];
while (location != -1)
{
Console.Write(" " + q.data[location]);
location = q.next[location];
}
}
}
public static void Main(String[] args)
{
NQueue q = new NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
Console.Write("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
Console.Write("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
Console.Write("\n Remove element of Queue " + 3 + " is : " + q.dequeue(3));
Console.Write("\n Remove element of Queue " + 1 + " is : " + q.dequeue(1));
}
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````<?php
/*
Php Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
public \$element;
public \$part;
public \$data;
public \$front;
public \$tail;
public \$next;
public \$counter;

function __construct(\$element, \$part)
{
\$this->element = \$element;
\$this->part = \$part;
\$this->counter = 0;
// Create memory of queue elements
\$this->data = array_fill(0, \$element, 0);
\$this->next = array_fill(0, \$element, 0);
\$this->front = array_fill(0, \$part, 0);
\$this->tail = array_fill(0, \$part, 0);
\$i = 0;
// Set the initial value of front and tail of queue
for (\$i = 0; \$i < \$part; ++\$i)
{
\$this->front[\$i] = -1;
\$this->tail[\$i] = -1;
}
// Set next and data value
for (\$i = 0; \$i < \$element - 1; ++\$i)
{
\$this->next[\$i] = \$i + 1;
}
\$this->next[\$element - 1] = -1;
}
// Determine that given queue is empty or not
public	function isEmpty(\$selection)
{
return \$this->front[\$selection] == -1;
}
// Determine that given queue is full or not
public	function isFull()
{
if (\$this->counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
public	function enqueue(\$selection, \$data)
{
// When given queue is out of range
if (\$selection < 0 || \$selection >= \$this->part)
{
return;
}
if (\$this->isFull() == true)
{
echo "\n Queue is Full \n";
}
else
{
\$location = \$this->next[\$this->counter];
if (\$this->isEmpty(\$selection) == true)
{
// First element of queue
\$this->front[\$selection] = \$this->counter;
\$this->tail[\$selection] = \$this->counter;
}
else
{
\$this->next[\$this->tail[\$selection]] = \$this->counter;
\$this->tail[\$selection] = \$this->counter;
}
\$this->data[\$this->counter] = \$data;
\$this->next[\$this->counter] = -1;
\$this->counter = \$location;
}
}
// Handles the request of remove selected queue element
public	function dequeue(\$selection)
{
if (\$selection < 0 || \$selection >= \$this->part)
{
// When given queue is out of range
return -1;
}
else if (\$this->isEmpty(\$selection) == true)
{
echo "\n Queue is Empty \n";
}
else
{
// Get the removed element location
\$location = \$this->front[\$selection];
// Get the removed element
\$item = \$this->data[\$this->front[\$selection]];
\$this->data[\$this->front[\$selection]] = 0;
// reset data
// Front is move to next element of queue
\$this->front[\$selection] = \$this->next[\$location];
// Set empty location in remove element
\$this->next[\$location] = \$this->counter;
// Set counter to current empty location
\$this->counter = \$location;
return \$item;
}
return -1;
}
}
class Implementation
{
// Display queue elements
public	function printQueue(\$q)
{
\$location = 0;
for (\$i = 0; \$i < \$q->part; ++\$i)
{
echo "\n Queue ". \$i ." : ";
// Select Queue
\$location = \$q->front[\$i];
while (\$location != -1)
{
echo " ". \$q->data[\$location];
\$location = \$q->next[\$location];
}
}
}
}

function main()
{
\$q = new NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
\$q->enqueue(0, 5);
// Put data in queue-1
\$q->enqueue(1, 10);
// Put data in queue-2
\$q->enqueue(2, 20);
// Put data in queue-3
\$q->enqueue(3, 30);
\$q->enqueue(3, 19);
// Put data in queue-0
\$q->enqueue(0, 15);
\$q->enqueue(0, 25);
// Put data in queue-3
\$q->enqueue(3, 60);
\$q->enqueue(3, 9);
\$q->enqueue(3, 3);
// Put data in queue-2
\$q->enqueue(2, 8);
\$q->enqueue(2, 45);
// Perform remove operation
echo "\n Remove element of Queue ". 0 ." is : ". \$q->dequeue(0);
echo "\n Remove element of Queue ". 0 ." is : ". \$q->dequeue(0);
echo "\n Remove element of Queue ". 3 ." is : ". \$q->dequeue(3);
echo "\n Remove element of Queue ". 1 ." is : ". \$q->dequeue(1);
}
main();``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````/*
Node Js Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
constructor(element, part)
{
this.element = element;
this.part = part;
this.counter = 0;
// Create memory of queue elements
this.data = Array(element).fill(0);
this.next = Array(element).fill(0);
this.front = Array(part).fill(-1);
this.tail = Array(part).fill(-1);
var i = 0;

// Set next and data value
for (i = 0; i < element - 1; ++i)
{
this.next[i] = i + 1;
}
this.next[element - 1] = -1;
}
// Determine that given queue is empty or not
isEmpty(selection)
{
return this.front[selection] == -1;
}
// Determine that given queue is full or not
isFull()
{
if (this.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
enqueue(selection, data)
{
// When given queue is out of range
if (selection < 0 || selection >= this.part)
{
return;
}
if (this.isFull() == true)
{
process.stdout.write("\n Queue is Full \n");
}
else
{
var location = this.next[this.counter];
if (this.isEmpty(selection) == true)
{
// First element of queue
this.front[selection] = this.counter;
this.tail[selection] = this.counter;
}
else
{
this.next[this.tail[selection]] = this.counter;
this.tail[selection] = this.counter;
}
this.data[this.counter] = data;
this.next[this.counter] = -1;
this.counter = location;
}
}
// Handles the request of remove selected queue element
dequeue(selection)
{
if (selection < 0 || selection >= this.part)
{
// When given queue is out of range
return -1;
}
else if (this.isEmpty(selection) == true)
{
process.stdout.write("\n Queue is Empty \n");
}
else
{
// Get the removed element location
var location = this.front[selection];
// Get the removed element
var item = this.data[this.front[selection]];
this.data[this.front[selection]] = 0;
// reset data
// Front is move to next element of queue
this.front[selection] = this.next[location];
// Set empty location in remove element
this.next[location] = this.counter;
// Set counter to current empty location
this.counter = location;
return item;
}
return -1;
}
}
class Implementation
{
// Display queue elements
printQueue(q)
{
var location = 0;
for (var i = 0; i < q.part; ++i)
{
process.stdout.write("\n Queue " + i + " : ");
// Select Queue
location = q.front[i];
while (location != -1)
{
process.stdout.write(" " + q.data[location]);
location = q.next[location];
}
}
}
}

function main()
{
var q = new NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
process.stdout.write("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
process.stdout.write("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
process.stdout.write("\n Remove element of Queue " + 3 + " is : " + q.dequeue(3));
process.stdout.write("\n Remove element of Queue " + 1 + " is : " + q.dequeue(1));
}
main();``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````#   Python 3 Program for
#   Efficiently implement n queues in a single array

class NQueue :

def __init__(self, element, part) :
self.element = element
self.part = part
self.counter = 0
#  Create memory of queue elements
self.data = [0] * (element)
self.next = [0] * (element)
self.front = [-1] * (part)
self.tail = [-1] * (part)
i = 0
#  Set next and data value
while (i < element - 1) :
self.next[i] = i + 1
i += 1

self.next[element - 1] = -1

#  Determine that given queue is empty or not
def isEmpty(self, selection) :
return self.front[selection] == -1

#  Determine that given queue is full or not
def isFull(self) :
if (self.counter == -1) :
return True

return False

#  Handles the request of adding a new element into select queue
def enqueue(self, selection, data) :
#  When given queue is out of range
if (selection < 0 or selection >= self.part) :
return

if (self.isFull() == True) :
print("\n Queue is Full ")
else :
location = self.next[self.counter]
if (self.isEmpty(selection) == True) :
#  First element of queue
self.front[selection] = self.counter
self.tail[selection] = self.counter
else :
self.next[self.tail[selection]] = self.counter
self.tail[selection] = self.counter

self.data[self.counter] = data
self.next[self.counter] = -1
self.counter = location

#  Handles the request of remove selected queue element
def dequeue(self, selection) :
if (selection < 0 or selection >= self.part) :
#  When given queue is out of range
return -1

elif(self.isEmpty(selection) == True) :
print("\n Queue is Empty ")
else :
#  Get the removed element location
location = self.front[selection]
#  Get the removed element
item = self.data[self.front[selection]]
self.data[self.front[selection]] = 0
#  reset data
#  Front is move to next element of queue
self.front[selection] = self.next[location]
#  Set empty location in remove element
self.next[location] = self.counter
#  Set counter to current empty location
self.counter = location
return item

return -1

class Implementation :
#  Display queue elements
def printQueue(self, q) :
location = 0
i = 0
while (i < q.part) :
print("\n Queue ", i ," : ", end = "")
#  Select Queue
location = q.front[i]
while (location != -1) :
print(" ", q.data[location], end = "")
location = q.next[location]

i += 1

def main() :
q = NQueue(15, 4)
#  Add element in queue using random queue selection
#  Put data in queue-0
q.enqueue(0, 5)
#  Put data in queue-1
q.enqueue(1, 10)
#  Put data in queue-2
q.enqueue(2, 20)
#  Put data in queue-3
q.enqueue(3, 30)
q.enqueue(3, 19)
#  Put data in queue-0
q.enqueue(0, 15)
q.enqueue(0, 25)
#  Put data in queue-3
q.enqueue(3, 60)
q.enqueue(3, 9)
q.enqueue(3, 3)
#  Put data in queue-2
q.enqueue(2, 8)
q.enqueue(2, 45)
#  Perform remove operation
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0), end = "")
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0), end = "")
print("\n Remove element of Queue ", 3 ," is : ", q.dequeue(3), end = "")
print("\n Remove element of Queue ", 1 ," is : ", q.dequeue(1), end = "")

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

Output

`````` Queue  0  :   5  15  25
Queue  1  :   10
Queue  2  :   20  8  45
Queue  3  :   30  19  60  9  3
Remove element of Queue  0  is :  5
Remove element of Queue  0  is :  15
Remove element of Queue  3  is :  30
Remove element of Queue  1  is :  10
Queue  0  :   25
Queue  1  :
Queue  2  :   20  8  45
Queue  3  :   19  60  9  3``````
``````#   Ruby Program for
#   Efficiently implement n queues in a single array

class NQueue
# Define the accessor and reader of class NQueue
attr_reader :element, :part, :data, :front, :tail, :next, :counter
attr_accessor :element, :part, :data, :front, :tail, :next, :counter

def initialize(element, part)
self.element = element
self.part = part
self.counter = 0
#  Create memory of queue elements
self.data = Array.new(element) {0}
self.next = Array.new(element) {0}
self.front = Array.new(part) {-1}
self.tail = Array.new(part) {-1}
i = 0
#  Set next and data value
while (i < element - 1)
self.next[i] = i + 1
i += 1
end

self.next[element - 1] = -1
end

#  Determine that given queue is empty or not
def isEmpty(selection)
return self.front[selection] == -1
end

#  Determine that given queue is full or not
def isFull()
if (self.counter == -1)
return true
end

return false
end

#  Handles the request of adding a new element into select queue
def enqueue(selection, data)
#  When given queue is out of range
if (selection < 0 || selection >= self.part)
return
end

if (self.isFull() == true)
print("\n Queue is Full \n")
else
location = self.next[self.counter]
if (self.isEmpty(selection) == true)
#  First element of queue
self.front[selection] = self.counter
self.tail[selection] = self.counter
else
self.next[self.tail[selection]] = self.counter
self.tail[selection] = self.counter
end

self.data[self.counter] = data
self.next[self.counter] = -1
self.counter = location
end

end

#  Handles the request of remove selected queue element
def dequeue(selection)
if (selection < 0 || selection >= self.part)
#  When given queue is out of range
return -1
elsif(self.isEmpty(selection) == true)
print("\n Queue is Empty \n")
else
#  Get the removed element location
location = self.front[selection]
#  Get the removed element
item = self.data[self.front[selection]]
self.data[self.front[selection]] = 0
#  reset data
#  Front is move to next element of queue
self.front[selection] = self.next[location]
#  Set empty location in remove element
self.next[location] = self.counter
#  Set counter to current empty location
self.counter = location
return item
end

return -1
end

end

class Implementation
#  Display queue elements
def printQueue(q)
location = 0
i = 0
while (i < q.part)
print("\n Queue ", i ," : ")
#  Select Queue
location = q.front[i]
while (location != -1)
print(" ", q.data[location])
location = q.next[location]
end

i += 1
end

end

end

def main()
q = NQueue.new(15, 4)
#  Add element in queue using random queue selection
#  Put data in queue-0
q.enqueue(0, 5)
#  Put data in queue-1
q.enqueue(1, 10)
#  Put data in queue-2
q.enqueue(2, 20)
#  Put data in queue-3
q.enqueue(3, 30)
q.enqueue(3, 19)
#  Put data in queue-0
q.enqueue(0, 15)
q.enqueue(0, 25)
#  Put data in queue-3
q.enqueue(3, 60)
q.enqueue(3, 9)
q.enqueue(3, 3)
#  Put data in queue-2
q.enqueue(2, 8)
q.enqueue(2, 45)
#  Perform remove operation
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0))
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0))
print("\n Remove element of Queue ", 3 ," is : ", q.dequeue(3))
print("\n Remove element of Queue ", 1 ," is : ", q.dequeue(1))
end

main()``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````/*
Scala Program for
Efficiently implement n queues in a single array
*/
class NQueue(var element: Int , var part: Int ,
var data: Array[Int] , var front: Array[Int] ,
var tail: Array[Int] , var next: Array[Int] , var counter: Int)
{
def this(element: Int, part: Int)
{
this(element, part,
Array.fill[Int](element)(0),
Array.fill[Int](part)(-1),
Array.fill[Int](part)(-1),
Array.fill[Int](element)(0), 0);
var i: Int = 0
// Set next and data value
while (i < element - 1)
{
this.next(i) = i+1;
i += 1
}
this.next(element - 1) = -1;
}
// Determine that given queue is empty or not
def isEmpty(selection: Int): Boolean = {
return this.front(selection) == -1;
}
// Determine that given queue is full or not
def isFull(): Boolean = {
if (this.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
def enqueue(selection: Int, data: Int): Unit = {
// When given queue is out of range
if (selection < 0 || selection >= this.part)
{
return;
}
if (this.isFull() == true)
{
print("\n Queue is Full \n");
}
else
{
var location: Int = this.next(this.counter);
if (this.isEmpty(selection) == true)
{
// First element of queue
this.front(selection) = this.counter;
this.tail(selection) = this.counter;
}
else
{
this.next(this.tail(selection)) = this.counter;
this.tail(selection) = this.counter;
}
this.data(this.counter) = data;
this.next(this.counter) = -1;
this.counter = location;
}
}
// Handles the request of remove selected queue element
def dequeue(selection: Int): Int = {
if (selection < 0 || selection >= this.part)
{
// When given queue is out of range
return -1;
}
else if (this.isEmpty(selection) == true)
{
print("\n Queue is Empty \n");
}
else
{
// Get the removed element location
var location: Int = this.front(selection);
// Get the removed element
var item: Int = this.data(this.front(selection));
this.data(this.front(selection)) = 0;
// reset data
// Front is move to next element of queue
this.front(selection) = this.next(location);
// Set empty location in remove element
this.next(location) = this.counter;
// Set counter to current empty location
this.counter = location;
return item;
}
return -1;
}
}
class Implementation
{
// Display queue elements
def printQueue(q: NQueue): Unit = {
var location: Int = 0;
var i: Int = 0;
while (i < q.part)
{
print("\n Queue " + i + " : ");
// Select Queue
location = q.front(i);
while (location != -1)
{
print(" " + q.data(location));
location = q.next(location);
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var q: NQueue = new NQueue(15, 4);
var task: Implementation = new Implementation();
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
print("\n Remove element of Queue " + 3 + " is : " + q.dequeue(3));
print("\n Remove element of Queue " + 1 + " is : " + q.dequeue(1));
}
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````
``````/*
Swift 4 Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
var element: Int;
var part: Int;
var data: [Int];
var front: [Int];
var tail: [Int];
var next: [Int];
var counter: Int;
init(_ element: Int, _ part: Int)
{
self.element = element;
self.part = part;
self.counter = 0;
// Create memory of queue elements
self.data = Array(repeating: 0, count: element);
self.next = Array(repeating: 0, count: element);
self.front = Array(repeating: -1, count: part);
self.tail = Array(repeating: -1, count: part);
var i: Int = 0;
// Set next and data value
while (i < element - 1)
{
self.next[i] = i + 1;
i += 1;
}
self.next[element - 1] = -1;
}
// Determine that given queue is empty or not
func isEmpty(_ selection: Int)->Bool
{
return self.front[selection] == -1;
}
// Determine that given queue is full or not
func isFull()->Bool
{
if (self.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
func enqueue(_ selection: Int, _ data: Int)
{
// When given queue is out of range
if (selection < 0 || selection >= self.part)
{
return;
}
if (self.isFull() == true)
{
print("\n Queue is Full ");
}
else
{
let location: Int = self.next[self.counter];
if (self.isEmpty(selection) == true)
{
// First element of queue
self.front[selection] = self.counter;
self.tail[selection] = self.counter;
}
else
{
self.next[self.tail[selection]] = self.counter;
self.tail[selection] = self.counter;
}
self.data[self.counter] = data;
self.next[self.counter] = -1;
self.counter = location;
}
}
// Handles the request of remove selected queue element
func dequeue(_ selection: Int)->Int
{
if (selection < 0 || selection >= self.part)
{
// When given queue is out of range
return -1;
}
else if (self.isEmpty(selection) == true)
{
print("\n Queue is Empty ");
}
else
{
// Get the removed element location
let location: Int = self.front[selection];
// Get the removed element
let item: Int = self.data[self.front[selection]];
self.data[self.front[selection]] = 0;
// reset data
// Front is move to next element of queue
self.front[selection] = self.next[location];
// Set empty location in remove element
self.next[location] = self.counter;
// Set counter to current empty location
self.counter = location;
return item;
}
return -1;
}
}
class Implementation
{
// Display queue elements
func printQueue(_ q: NQueue? )
{
var location: Int = 0;
var i: Int = 0;
while (i < q!.part)
{
print("\n Queue ", i ," : ", terminator: "");
// Select Queue
location = q!.front[i];
while (location  != -1)
{
print(" ", q!.data[location], terminator: "");
location = q!.next[location];
}
i += 1;
}
}
}
func main()
{
let q: NQueue = NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0), terminator: "");
print("\n Remove element of Queue ", 0 ," is : ", q.dequeue(0), terminator: "");
print("\n Remove element of Queue ", 3 ," is : ", q.dequeue(3), terminator: "");
print("\n Remove element of Queue ", 1 ," is : ", q.dequeue(1), terminator: "");
}
main();``````

Output

`````` Queue  0  :   5  15  25
Queue  1  :   10
Queue  2  :   20  8  45
Queue  3  :   30  19  60  9  3
Remove element of Queue  0  is :  5
Remove element of Queue  0  is :  15
Remove element of Queue  3  is :  30
Remove element of Queue  1  is :  10
Queue  0  :   25
Queue  1  :
Queue  2  :   20  8  45
Queue  3  :   19  60  9  3``````
``````/*
Kotlin Program for
Efficiently implement n queues in a single array
*/
class NQueue
{
var element: Int;
var part: Int;
var data: Array <Int> ;
var front: Array <Int> ;
var tail: Array <Int> ;
var next: Array <Int> ;
var counter: Int;
constructor(element: Int, part: Int)
{
this.element = element;
this.part = part;
this.counter = 0;
// Create memory of queue elements
this.data = Array(element)
{
0
};
this.next = Array(element)
{
0
};
this.front = Array(part)
{
-1
};
this.tail = Array(part)
{
-1
};
var i: Int = 0;
// Set next and data value
while (i < element - 1)
{
this.next[i] = i + 1;
i += 1;
}
this.next[element - 1] = -1;
}
// Determine that given queue is empty or not
fun isEmpty(selection: Int): Boolean
{
return this.front[selection] == -1;
}
// Determine that given queue is full or not
fun isFull(): Boolean
{
if (this.counter == -1)
{
return true;
}
return false;
}
// Handles the request of adding a new element into select queue
fun enqueue(selection: Int, data: Int): Unit
{
// When given queue is out of range
if (selection < 0 || selection >= this.part)
{
return;
}
if (this.isFull() == true)
{
print("\n Queue is Full \n");
}
else
{
var location: Int = this.next[this.counter];
if (this.isEmpty(selection) == true)
{
// First element of queue
this.front[selection] = this.counter;
this.tail[selection] = this.counter;
}
else
{
this.next[this.tail[selection]] = this.counter;
this.tail[selection] = this.counter;
}
this.data[this.counter] = data;
this.next[this.counter] = -1;
this.counter = location;
}
}
// Handles the request of remove selected queue element
fun dequeue(selection: Int): Int
{
if (selection < 0 || selection >= this.part)
{

// When given queue is out of range
return -1;
}
else if (this.isEmpty(selection) == true)
{
print("\n Queue is Empty \n");
}
else
{
// Get the removed element location
var location: Int = this.front[selection];
// Get the removed element
var item: Int = this.data[this.front[selection]];
this.data[this.front[selection]] = 0;
// reset data
// Front is move to next element of queue
this.front[selection] = this.next[location];
// Set empty location in remove element
this.next[location] = this.counter;
// Set counter to current empty location
this.counter = location;
return item;
}
return -1;
}
}
class Implementation
{
// Display queue elements
fun printQueue(q: NQueue): Unit
{
var location: Int;
var i: Int = 0;
while (i < q.part)
{
print("\n Queue " + i + " : ");
// Select Queue
location = q.front[i];
while (location != -1)
{
print(" " + q.data[location]);
location = q.next[location];
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
var q: NQueue = NQueue(15, 4);
// Add element in queue using random queue selection
// Put data in queue-0
q.enqueue(0, 5);
// Put data in queue-1
q.enqueue(1, 10);
// Put data in queue-2
q.enqueue(2, 20);
// Put data in queue-3
q.enqueue(3, 30);
q.enqueue(3, 19);
// Put data in queue-0
q.enqueue(0, 15);
q.enqueue(0, 25);
// Put data in queue-3
q.enqueue(3, 60);
q.enqueue(3, 9);
q.enqueue(3, 3);
// Put data in queue-2
q.enqueue(2, 8);
q.enqueue(2, 45);
// Perform remove operation
print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
print("\n Remove element of Queue " + 0 + " is : " + q.dequeue(0));
print("\n Remove element of Queue " + 3 + " is : " + q.dequeue(3));
print("\n Remove element of Queue " + 1 + " is : " + q.dequeue(1));
}``````

Output

`````` Queue 0 :  5 15 25
Queue 1 :  10
Queue 2 :  20 8 45
Queue 3 :  30 19 60 9 3
Remove element of Queue 0 is : 5
Remove element of Queue 0 is : 15
Remove element of Queue 3 is : 30
Remove element of Queue 1 is : 10
Queue 0 :  25
Queue 1 :
Queue 2 :  20 8 45
Queue 3 :  19 60 9 3``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.