close
The Wayback Machine - https://web.archive.org/web/20250126180215/https://www.geeksforgeeks.org/array-implementation-of-queue-simple/
Open In App

Array implementation of queue

Last Updated : 24 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Below is the illustration:


Below is the implementation of the above approach:

C++
// C++ program to implement
// queue using array.
#include <bits/stdc++.h>
using namespace std;

class Queue {
  public:
    vector<int> arr;
    int size;
    int capacity;
    int front;

    Queue(int qSize) {
        size = 0;
        front = 0;
        capacity = qSize;
        arr.resize(qSize);
    }

    // Function to add an element
    // to queue.
    void enqueue(int x) {

        // If queue is full
        if (size == capacity) {
            return;
        }

        arr[size] = x;

        // Increment queue size.
        size++;
    }

    // Function to pop front
    // element from queue.
    void dequeue() {

        // If queue is empty
        if (size == 0) {
            return;
        }

        // Shift all the elements
        // to the left.
        for (int i = 1; i < size; i++) {
            arr[i - 1] = arr[i];
        }

        // decrement queue size
        size--;
    }

    // Function which returns
    // the front element.
    int getFront() {

        // If queue is empty
        if (size == 0)
            return -1;

        return arr[front];
    }

    // Function which prints
    // the elements of array.
    void display() {

        for (int i = front; i < size; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};

int main() {
  
    Queue *q = new Queue(4);

    q->enqueue(1);
    q->enqueue(2);
    q->enqueue(3);
    cout << q->getFront() << endl;
    q->dequeue();
    q->enqueue(4);
    q->display();

    return 0;
}
Java
// Java program to implement 
// queue using array.

class Queue {
    int[] arr;
    int size;
    int capacity;
    int front;
    
    Queue(int qSize) {
        size = 0;
        front = 0;
        capacity = qSize;
        arr = new int[qSize];
    }
    
    // Function to add an element
    // to queue.
    void enqueue(int x) {
        
        // If queue is full
        if (size == capacity) {
            return;
        }
        
        arr[size] = x;
        
        // Increment queue size.
        size++;
    }
    
    // Function to pop front 
    // element from queue.
    void dequeue() {
        
        // If queue is empty 
        if (size == 0) {
            return;
        }
        
        // Shift all the elements 
        // to the left.
        for (int i = 1; i < size; i++) {
            arr[i-1] = arr[i];
        }
        
        // decrement queue size 
        size--;
    }
    
    // Function which returns 
    // the front element.
    int getFront() {
        
        // If queue is empty
        if (size == 0) return -1;
        
        return arr[front];
    }
    
    // Function which prints
    // the elements of array.
    void display() {
        
        for (int i = front; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

class GfG {
    public static void main(String[] args) {
        Queue q = new Queue(4);
        
        q.enqueue(1);
        q.enqueue(2);
        q.enqueue(3);
        System.out.println(q.getFront());
        q.dequeue();
        q.enqueue(4);
        q.display();
    }
}
Python
# Python program to implement 
# queue using array.

class Queue:
    def __init__(self, q_size):
        self.arr = [0] * q_size
        self.size = 0
        self.capacity = q_size
        self.front = 0
    
    # Function to add an element
    # to queue.
    def enqueue(self, x):
        
        # If queue is full
        if self.size == self.capacity:
            return
        
        self.arr[self.size] = x
        
        # Increment queue size.
        self.size += 1
    
    # Function to pop front 
    # element from queue.
    def dequeue(self):
        
        # If queue is empty 
        if self.size == 0:
            return
        
        # Shift all the elements 
        # to the left.
        for i in range(1, self.size):
            self.arr[i-1] = self.arr[i]
        
        # decrement queue size 
        self.size -= 1
    
    # Function which returns 
    # the front element.
    def getFront(self):
        
        # If queue is empty
        if self.size == 0:
            return -1
        
        return self.arr[self.front]
    
    # Function which prints
    # the elements of array.
    def display(self):
        
        for i in range(self.front, self.size):
            print(self.arr[i], end=" ")
        print()

if __name__ == "__main__":
    q = Queue(4)
    
    q.enqueue(1)
    q.enqueue(2)
    q.enqueue(3)
    print(q.getFront())
    q.dequeue()
    q.enqueue(4)
    q.display()
C#
// C# program to implement 
// queue using array.

class Queue {
    int[] arr;
    int size;
    int capacity;
    int front;
    
    public Queue(int qSize) {
        size = 0;
        front = 0;
        capacity = qSize;
        arr = new int[qSize];
    }
    
    // Function to add an element
    // to queue.
    public void Enqueue(int x) {
        
        // If queue is full
        if (size == capacity) {
            return;
        }
        
        arr[size] = x;
        
        // Increment queue size.
        size++;
    }
    
    // Function to pop front 
    // element from queue.
    public void Dequeue() {
        
        // If queue is empty 
        if (size == 0) {
            return;
        }
        
        // Shift all the elements 
        // to the left.
        for (int i = 1; i < size; i++) {
            arr[i-1] = arr[i];
        }
        
        // decrement queue size 
        size--;
    }
    
    // Function which returns 
    // the front element.
    public int GetFront() {
        
        // If queue is empty
        if (size == 0) return -1;
        
        return arr[front];
    }
    
    // Function which prints
    // the elements of array.
    public void Display() {
        
        for (int i = front; i < size; i++) {
            System.Console.Write(arr[i] + " ");
        }
        System.Console.WriteLine();
    }
}

class GfG {
    static void Main(string[] args) {
        Queue q = new Queue(4);
        
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(3);
        System.Console.WriteLine(q.GetFront());
        q.Dequeue();
        q.Enqueue(4);
        q.Display();
    }
}
JavaScript
// JavaScript program to implement 
// queue using array.

class Queue {
    constructor(qSize) {
        this.arr = new Array(qSize).fill(0);
        this.size = 0;
        this.capacity = qSize;
        this.front = 0;
    }
    
    // Function to add an element
    // to queue.
    enqueue(x) {
        
        // If queue is full
        if (this.size === this.capacity) {
            return;
        }
        
        this.arr[this.size] = x;
        
        // Increment queue size.
        this.size++;
    }
    
    // Function to pop front 
    // element from queue.
    dequeue() {
        
        // If queue is empty 
        if (this.size === 0) {
            return;
        }
        
        // Shift all the elements 
        // to the left.
        for (let i = 1; i < this.size; i++) {
            this.arr[i-1] = this.arr[i];
        }
        
        // decrement queue size 
        this.size--;
    }
    
    // Function which returns 
    // the front element.
    getFront() {
        
        // If queue is empty
        if (this.size === 0) return -1;
        
        return this.arr[this.front];
    }
    
    // Function which prints
    // the elements of array.
    display() {
        
        for (let i = this.front; i < this.size; i++) {
            console.log(this.arr[i] + " ");
        }
        console.log();
    }
}

//driver code
const q = new Queue(4);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
console.log(q.getFront());
q.dequeue();
q.enqueue(4);
q.display();

Output
1
2 3 4 

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.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg