Name some Queue implementations and compare them by efficiency of operations
Last Updated :
23 Jul, 2025
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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem