Name some Queue implementations and compare them by efficiency of operations
Last Updated :
30 Mar, 2023
A queue is a linear data structure in which insertion is done from one end called the rear end and deletion is done from another end called the front end. It follows FIFO (First In First Out) approach. The insertion in the queue is called enqueue operation and deletion in the queue is called dequeue operation.
A queue can be implemented in two ways:
- Array implementation of queue
- Linked List implementation of queue
Array implementation of the queue:
For the array implementation of the queue, we have to take an array of size n. We also use two pointers front and rear to keep track of the front element and the last position where a new element can be inserted respectively. All the functionalities are satisfied by using these two pointers. For more details about array implementation of a queue refer to this link.
Below is the code for array implementation of a queue.
C++
// C++ program to implement queue using array
#include <bits/stdc++.h>
using namespace std;
// Structure of a queue
struct Queue {
int rear, front, s;
int* q;
Queue(int c)
{
front = rear = 0;
s = c;
q = new int;
}
~Queue() { delete[] q; }
// Function to insert element at
// the rear end of the queue
void enqueue(int data)
{
if (rear == s)
cout << "Queue is full\n";
else {
q[rear] = data;
rear++;
}
return;
}
// Function to delete element at
// the front end of the queue
void dequeue()
{
if (rear == front)
cout << "Queue is empty\n";
else
front++;
}
// Function to display the queue
void display()
{
if (rear == front)
cout << "Queue is empty\n";
else
for (int i = front; i < rear; i++) {
cout << q[i] << " ";
}
}
};
// Driver code
int main()
{
Queue p(3);
p.enqueue(10);
p.enqueue(20);
p.enqueue(30);
p.display();
cout << "\nAfter two deletion\n";
p.dequeue();
p.dequeue();
p.display();
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Class of a queue
static class Queue {
int rear, front, s;
int q[];
// Constructor of a queue
Queue(int c)
{
front = 0;
rear = 0;
s = c;
q = new int[s];
}
// Function to insert element at
// the rear end of the queue
void enqueue(int data)
{
if (rear == s)
System.out.println("Queue is full");
else {
q[rear] = data;
rear++;
}
return;
}
// Function to delete element at
// the front end of the queue
void dequeue()
{
if (rear == front)
System.out.println("Queue is empty");
else
front++;
}
// Function to display the queue
void display()
{
if (rear == front)
System.out.println("Queue is empty");
else
for (int i = front; i < rear; i++) {
System.out.print(q[i]+" ");
}
}
}
public static void main (String[] args) {
Queue p = new Queue(3);
p.enqueue(10);
p.enqueue(20);
p.enqueue(30);
p.display();
System.out.println("\nAfter two deletion");
p.dequeue();
p.dequeue();
p.display();
}
}
// This code is contributed by aadityaburujwale.
Python3
# Structure of a queue
class Queue:
# Constructor of a queue
def __init__(self, c):
self.rear = 0
self.front = 0
self.s = c
self.q = [0] * c
# Function to insert element at
# the rear end of the queue
def enqueue(self, data):
if self.rear == self.s:
print("Queue is full")
else:
self.q[self.rear] = data
self.rear += 1
# Function to delete element at
# the front end of the queue
def dequeue(self):
if self.rear == self.front:
print("Queue is empty")
else:
self.front += 1
# Function to display the queue
def display(self):
if self.rear == self.front:
print("Queue is empty")
else:
for i in range(self.front, self.rear):
print(self.q[i], end=" ")
print()
# Driver code
if __name__ == "__main__":
p = Queue(3)
p.enqueue(10)
p.enqueue(20)
p.enqueue(30)
p.display()
print("After two deletion")
p.dequeue()
p.dequeue()
p.display()
# This code is contributed by akashish__
C#
// C# program to implement queue using array
using System;
public class GFG {
// Class of a queue
class Queue {
public int rear, front, s;
public int[] q;
// Constructor of a queue
public Queue(int c)
{
front = 0;
rear = 0;
s = c;
q = new int[s];
}
// Function to insert element at
// the rear end of the queue
public void enqueue(int data)
{
if (rear == s)
Console.WriteLine("Queue is full");
else {
q[rear] = data;
rear++;
}
return;
}
// Function to delete element at
// the front end of the queue
public void dequeue()
{
if (rear == front)
Console.WriteLine("Queue is empty");
else
front++;
}
// Function to display the queue
public void display()
{
if (rear == front)
Console.WriteLine("Queue is empty");
else
for (int i = front; i < rear; i++) {
Console.Write(q[i] + " ");
}
}
}
static public void Main()
{
Queue p = new Queue(3);
p.enqueue(10);
p.enqueue(20);
p.enqueue(30);
p.display();
Console.WriteLine("\nAfter two deletion");
p.dequeue();
p.dequeue();
p.display();
}
}
// This code is contributed by lokesh.
JavaScript
// JS program to implement queue using array
// Structure of a queue
class Queue {
constructor() {
this.rear = 0;
this.front = 0;
this.q = new Array;
}
// Function to insert element at
// the rear end of the queue
enqueue(data) {
if (this.rear == 3)
console.log("Queue is full");
else {
this.q[this.rear] = data;
this.rear++;
}
return;
}
// Function to delete element at
// the front end of the queue
dequeue() {
if (this.rear == this.front)
console.log("Queue is empty");
else
this.front++;
}
// Function to display the queue
display() {
if (this.rear == this.front)
console.log("Queue is empty");
else
for (let i = this.front; i < this.rear; i++) {
console.log(this.q[i], " ");
}
}
};
// Driver code
let p = new Queue;
p.enqueue(10);
p.enqueue(20);
p.enqueue(30);
p.display();
console.log("After two deletion");
p.dequeue();
p.dequeue();
p.display();
// This code is contributed by adityamaharshi21
Output10 20 30
After two deletion
30
Time Complexity:
Insertion: O(1)
Deletion: O(1)
Searching: O(n)
Space Complexity: O(n)
Linked List Implementation of the queue:
For implementing a queue using linked list we don't need to know the size beforehand like array. The dynamic property of linked list allows queue to grow to any size. In case of a linked list also we use two pointers front and rear that perform the same task as in array. For more details about linked list implementation refer to this link.
Below is the code for linked list implementation of queue.
C++
// Program to implement queue using linked list
#include <bits/stdc++.h>
using namespace std;
// Structure of a queue node
struct Qnode {
int d;
struct Qnode* next;
};
// Structure of a queue
struct Q {
struct Qnode *front, *rear;
};
// Function to create a new node
struct Qnode* newNode(int k)
{
struct Qnode* t
= (struct Qnode*)malloc(sizeof(struct Qnode));
t->d = k;
t->next = NULL;
return t;
}
// Function to create a queue
struct Q* createQ()
{
struct Q* q = (struct Q*)malloc(sizeof(struct Q));
q->front = q->rear = NULL;
return q;
}
// Function to enqueue a new value
void enqueue(struct Q* q, int data)
{
struct Qnode* t = newNode(data);
if (q->rear == NULL) {
q->front = q->rear = t;
return;
}
q->rear->next = t;
q->rear = t;
}
// Function for implementing deque
void dequeue(struct Q* q)
{
if (q->front == NULL)
return;
struct Qnode* t = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(t);
}
// Driver code
int main()
{
struct Q* q = createQ();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
dequeue(q);
cout << "Queue front " << q->front->d;
cout << "\nQueue rear " << q->rear->d;
return 0;
}
Java
class Qnode {
int d;
Qnode next;
}
class Q {
Qnode front, rear;
Q() {
front = rear = null;
}
}
class Main {
static Qnode newNode(int k) {
Qnode t = new Qnode();
t.d = k;
t.next = null;
return t;
}
static Q createQ() {
Q q = new Q();
return q;
}
static void enqueue(Q q, int data) {
Qnode t = newNode(data);
if (q.rear == null) {
q.front = q.rear = t;
return;
}
q.rear.next = t;
q.rear = t;
}
static void dequeue(Q q) {
if (q.front == null) {
return;
}
Qnode t = q.front;
q.front = q.front.next;
if (q.front == null) {
q.rear = null;
}
t = null;
}
public static void main(String[] args) {
Q q = createQ();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
dequeue(q);
System.out.println("Queue front: " + q.front.d);
System.out.println("Queue rear: " + q.rear.d);
}
}
Python
class Qnode:
def __init__(self, d):
self.d = d
self.next = None
class Q:
def __init__(self):
self.front = None
self.rear = None
def newNode(k):
t = Qnode(k)
return t
def createQ():
q = Q()
return q
def enqueue(q, data):
t = newNode(data)
if q.rear is None:
q.front = q.rear = t
return
q.rear.next = t
q.rear = t
def dequeue(q):
if q.front is None:
return
t = q.front
q.front = q.front.next
if q.front is None:
q.rear = None
t = None
q = createQ()
enqueue(q, 10)
enqueue(q, 20)
enqueue(q, 30)
dequeue(q)
print("Queue front: ", q.front.d)
print("Queue rear: ", q.rear.d)
C#
public class Qnode
{
public int d;
public Qnode next;
public Qnode(int val)
{
d = val;
next = null;
}
}
public class Q
{
public Qnode front, rear;
public Q()
{
front = rear = null;
}
}
public class MainClass
{
public static Qnode newNode(int k)
{
Qnode t = new Qnode(k);
return t;
}
public static Q createQ()
{
Q q = new Q();
return q;
}
public static void enqueue(Q q, int data)
{
Qnode t = newNode(data);
if (q.rear == null)
{
q.front = q.rear = t;
return;
}
q.rear.next = t;
q.rear = t;
}
public static void dequeue(Q q)
{
if (q.front == null)
{
return;
}
Qnode t = q.front;
q.front = q.front.next;
if (q.front == null)
{
q.rear = null;
}
t = null;
}
public static void Main(string[] args)
{
Q q = createQ();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
dequeue(q);
System.Console.WriteLine("Queue front: " + q.front.d);
System.Console.WriteLine("Queue rear: " + q.rear.d);
}
}
// This code is contributed by factworx412.
JavaScript
// Program to implement queue using linked list
class QNode {
constructor(data) {
this.d = data;
this.next = null;
}
}
class Q {
constructor() {
this.front = this.rear = null;
}
}
const newNode = data => new QNode(data);
const createQ = () => new Q();
const enqueue = (q, data) => {
const t = newNode(data);
if (q.rear === null) {
q.front = q.rear = t;
return;
}
q.rear.next = t;
q.rear = t;
};
const dequeue = q => {
if (q.front === null) return;
const t = q.front;
q.front = q.front.next;
if (q.front === null) q.rear = null;
};
// Driver code
const q = createQ();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
dequeue(q);
console.log("Queue front: ", q.front.d);
console.log("Queue rear: ", q.rear.d);
// This code is contributed by aadityamaharshi21.
OutputQueue front 20
Queue rear 30
Time complexity: The time complexity of enqueue and dequeue operations in a queue implemented using linked list is O(1). This is because, in a linked list, insertion and deletion operations are performed in constant time.
Space complexity: The space complexity of a queue implemented using linked list is O(n), where n is the number of elements in the queue. This is because we need to allocate memory for each element in the queue and the size of the queue increases or decreases as elements are added or removed.
Note: For easy understanding only the enqueue() and deque() functionalities are shown here. For detailed implementation you can check the links provided for implementation.
Comparison:
Queue Operations | Array Implementation | Linked-List Implementation |
---|
Time Complexity | Space Complexity | Time Complexity | Space Complexity |
---|
Enqueue | O (1) | O (1) | O (1) | O (1) |
---|
Dequeue | O (1) | O (1) | O (1) | O (1) |
---|
IsFull | O (1) | O (1) | O (N) | O (1) |
---|
IsEmpty | O (1) | O (1) | O (1) | O (1) |
---|
Peek | O (1) | O (1) | O (1) | O (1) |
---|
Related articles:
Similar Reads
Difference between PriorityQueue and Queue Implementation in Java
Java Queue InterfaceThe Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed f
5 min read
Most efficient way to implement Stack and Queue together
Introduction to Stack:A stack is a linear data structure in computer science that follows the Last-In-First-Out (LIFO) principle. It is a data structure in which the insertion and removal of elements can only be performed at one end, which is called the top of the stack.In a stack, elements are push
15+ min read
Implement the insert and delete functions on Priority queue without Array
A priority Queue is a type of queue in which every element is associated with a priority and is served according to its priority. We will use two popular data structures for implementing priority queues without arrays - Fibonacci HeapBinomial HeapFibonacci Heap:Fibonacci heap is a heap data structur
15+ min read
Time and Space Complexity Analysis of Queue operations
What is Queue?Queue is a linear data structure that follows FIFO approach (First In First Out). One can imagine a queue as a line of people waiting in sequential order which starts from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as the rea
15+ min read
Introduction and Array Implementation of Queue
Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. The order is First In First Out (FIFO). One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginn
2 min read
Interchange elements of Stack and Queue without changing order
Given a stack St of M elements and a queue Q of N elements. The task is to put every element of stack into the queue and every element of the queue into the stack without changing their order. Examples: Input: St = {4, 3, 2, 1}, Q = {8, 7, 6, 5}Output: St = {8, 7, 6, 5}, Q = {1, 2, 3, 4} Input: St =
11 min read
Array implementation of queue - Simple
Please note that a simple array implementation discussed here is not used in practice as it is not efficient. In practice, we either use Linked List Implementation of Queue or circular array implementation of queue. The idea of this post is to give you a background as to why we need a circular array
5 min read
Introduction and Array Implementation of Deque
A Deque (Double-Ended Queue) is a data structure that allows elements to be added or removed from both endsâfront and rear. Unlike a regular queue, which only allows insertions at the rear and deletions from the front, a deque provides flexibility by enabling both operations at either end. This make
3 min read
Indexed Priority Queue with Implementation
Priority queue is a data structure in which data is stored on basis of its priority. In an Indexed Priority Queue, data is stored just like standard priority queue and along with this, the value of a data can be updated using its key. It is called "indexed" because a hash map can be used to store th
8 min read
Circular Array Implementation of Queue
A Circular Queue is a way of implementing a normal queue where the last element of the queue is connected to the first element of the queue forming a circle. The operations are performed based on the FIFO (First In First Out) principle. It is also called 'Ring Buffer'. In a normal Queue, we can inse
8 min read