Array implementation of queue - Simple
Last Updated :
11 Jul, 2025
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 implementation.
- The queue uses an array with a fixed capacity, referred to as capacity, and tracks the current number of elements with a variable size.
- The variable front is initialized to 0 and represents the index of the first element in the array. In the dequeue operation, the element at this index is removed.
To implement a queue of size n using an array, the operations are as follows:
- Enqueue: Adds new elements to the end of the queue. Checks if the queue has space before insertion, then increments the size.
- Dequeue: Removes the front element by shifting all remaining elements one position to the left. Decrements the queue size after removal.
- getFront: Returns the first element of the queue if it's not empty. Returns -1 if the queue is empty.
- Display: Iterates through the queue from the front to the current size and prints each element.
C++
#include <iostream>
#include <vector>
using namespace std;
class Queue {
vector<int> q;
public:
bool isEmpty() { return q.empty(); }
void enqueue(int x) {
q.push_back(x);
}
void dequeue() {
if (!isEmpty()) q.erase(q.begin());
}
int getFront() {
return isEmpty() ? -1 : q.front();
}
void display() {
for (int x : q) cout << x << " ";
cout << "\n";
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << q.getFront() << endl;
q.dequeue();
q.enqueue(4);
q.display();
}
C
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int *arr;
int front;
int rear;
int capacity;
};
struct Queue* createQueue(int capacity) {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = 0;
queue->rear = -1;
queue->arr = (int*)malloc(capacity * sizeof(int));
return queue;
}
int isEmpty(struct Queue* queue) {
return queue->front > queue->rear;
}
void enqueue(struct Queue* queue, int x) {
if (queue->rear < queue->capacity - 1) {
queue->arr[++queue->rear] = x;
}
}
void dequeue(struct Queue* queue) {
if (!isEmpty(queue)) {
queue->front++;
}
}
int getFront(struct Queue* queue) {
return isEmpty(queue) ? -1 : queue->arr[queue->front];
}
void display(struct Queue* queue) {
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->arr[i]);
}
printf("\n");
}
int main() {
struct Queue* q = createQueue(100);
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
printf("%d\n", getFront(q));
dequeue(q);
enqueue(q, 4);
display(q);
return 0;
}
Java
// Java implementation of Queue
import java.util.LinkedList;
import java.util.Queue;
class MyQueue {
private Queue<Integer> q = new LinkedList<>();
public boolean isEmpty() { return q.isEmpty(); }
public void enqueue(int x) { q.add(x); }
public void dequeue() { if (!isEmpty()) q.poll(); }
public int getFront() { return isEmpty() ? -1 : q.peek(); }
public void display() { for (int x : q) System.out.print(x + " "); System.out.println(); }
}
public class Main {
public static void main(String[] args) {
MyQueue q = new MyQueue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
System.out.println(q.getFront());
q.dequeue();
q.enqueue(4);
q.display();
}
}
Python
# Python implementation of Queue
class Queue:
def __init__(self):
self.q = []
def is_empty(self):
return len(self.q) == 0
def enqueue(self, x):
self.q.append(x)
def dequeue(self):
if not self.is_empty():
self.q.pop(0)
def get_front(self):
return -1 if self.is_empty() else self.q[0]
def display(self):
print(' '.join(map(str, self.q)))
if __name__ == '__main__':
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
print(q.get_front())
q.dequeue()
q.enqueue(4)
q.display()
C#
// C# implementation of Queue
using System;
using System.Collections.Generic;
class Queue {
private List<int> q = new List<int>();
public bool IsEmpty() { return q.Count == 0; }
public void Enqueue(int x) { q.Add(x); }
public void Dequeue() { if (!IsEmpty()) q.RemoveAt(0); }
public int GetFront() { return IsEmpty() ? -1 : q[0]; }
public void Display() { Console.WriteLine(string.Join(" ", q)); }
}
class Program {
static void Main() {
Queue q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
Console.WriteLine(q.GetFront());
q.Dequeue();
q.Enqueue(4);
q.Display();
}
}
JavaScript
class Queue {
constructor() {
this.q = [];
}
isEmpty() {
return this.q.length === 0;
}
enqueue(x) {
this.q.push(x);
}
dequeue() {
if (!this.isEmpty()) this.q.shift();
}
getFront() {
return this.isEmpty() ? -1 : this.q[0];
}
display() {
console.log(this.q.join(' '));
}
}
const q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
console.log(q.getFront());
q.dequeue();
q.enqueue(4);
q.display();
Time Complexity: O(1) for Enqueue (element insertion in the queue) as we simply increment pointer and put value in array, O(n) for Dequeue (element removing from the queue).
Auxiliary Space: O(n), as here we are using an n size array for implementing Queue
We can notice that the Dequeue operation is O(n) which is not acceptable. The enqueue and dequeue both operations should have O(1) time complexity. That is why if we wish to implement a queue using array (because of array advantages like cache friendliness and random access), we do circular array implementation of queue.
Similar Reads
implement k Queues in a single array Given an array of size n, the task is to implement k queues using the array.enqueue(qn, x) : Adds the element x into the queue number qn dequeue(qn, x) : Removes the front element from queue number qn isFull(qn) : Checks if the queue number qn is fullisEmpty(qn) : Checks if the queue number qn is em
15+ min read
implement k Queues in a single array Given an array of size n, the task is to implement k queues using the array.enqueue(qn, x) : Adds the element x into the queue number qn dequeue(qn, x) : Removes the front element from queue number qn isFull(qn) : Checks if the queue number qn is fullisEmpty(qn) : Checks if the queue number qn is em
15+ 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 inser
8 min read
Implementation of Deque using Array - Simple A Deque (Double-Ended Queue) is a data structure that allows insertion and deletion of elements at both ends (front and rear). This flexibility makes it more versatile than a regular queue, where insertion and deletion can only happen at one end. In this article, we will explore how to implement a d
7 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
Implementation of Deque using circular array Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and delete at both ends.Operations on Deque:Â Mainly the following four basic operations are performed on queue:Â insertFront(): Adds an item at the front of Deque.insertRear(): Adds an item at the rear
10 min read