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


Output

10 20 30 
After two deletion
30 

Time Complexity:

Insertion: O(1)
Deletion: O(1)
Searching: O(n)

Space Complexity: O(n)

Name some Queue implementations and compare them by efficiency of operations

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

Similar Reads

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....

Linked List Implementation of the queue:

...

Comparison:

...