How to implement Queue using Array?
To implement a queue using an array,
- create an array arr of size n and
- take two variables front and rear both of which will be initialized to 0 which means the queue is currently empty.
- Element
- rear is the index up to which the elements are stored in the array and
- front is the index of the first element of the array.
Now, some of the implementations of queue operations are as follows:
- Enqueue: Addition of an element to the queue. Adding an element will be performed after checking whether the queue is full or not. If rear < n which indicates that the array is not full then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be an Overflow condition as the array is full.
- Dequeue: Removal of an element from the queue. An element can only be deleted when there is at least an element to delete i.e. rear > 0. Now, the element at arr[front] can be deleted but all the remaining elements have to shift to the left by one position in order for the dequeue operation to delete the second element from the left on another dequeue operation.
- Front: Get the front element from the queue i.e. arr[front] if the queue is not empty.
- Display: Print all elements of the queue. If the queue is non-empty, traverse and print all the elements from the index front to rear.

Below is the implementation of a queue using an array:
In the below code , we are initializing front and rear as 0, but in general we have to initialize it with -1.
If we assign rear as 0, rear will always point to next block of the end element, in fact , rear should point the index of last element,
eg. When we insert element in queue , it will add in the end i.e. after the current rear and then point the rear to the new element ,
According to the following code:
IN the first dry run, front=rear = 0;
in void queueEnqueue(int data)
else part will be executed,
so arr[rear] =data;// rear =0, rear pointing to the latest element
rear++; //now rear = 1, rear pointing to the next block after end element not the end element
//that’s against the original definition of rear
C++
#include <bits/stdc++.h>
using namespace std;
struct Queue {
int front, rear, capacity;
int * queue;
Queue( int c)
{
front = rear = 0;
capacity = c;
queue = new int ;
}
~Queue() { delete [] queue; }
void queueEnqueue( int data)
{
if (capacity == rear) {
printf ( "\nQueue is full\n" );
return ;
}
else {
queue[rear] = data;
rear++;
}
return ;
}
void queueDequeue()
{
if (front == rear) {
printf ( "\nQueue is empty\n" );
return ;
}
else {
for ( int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
rear--;
}
return ;
}
void queueDisplay()
{
int i;
if (front == rear) {
printf ( "\nQueue is Empty\n" );
return ;
}
for (i = front; i < rear; i++) {
printf ( " %d <-- " , queue[i]);
}
return ;
}
void queueFront()
{
if (front == rear) {
printf ( "\nQueue is Empty\n" );
return ;
}
printf ( "\nFront Element is: %d" , queue[front]);
return ;
}
};
int main( void )
{
Queue q(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
printf ( "\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
return 0;
}
|
Java
class Queue {
static private int front, rear, capacity;
static private int queue[];
Queue( int c)
{
front = rear = 0 ;
capacity = c;
queue = new int [capacity];
}
static void queueEnqueue( int data)
{
if (capacity == rear) {
System.out.printf( "\nQueue is full\n" );
return ;
}
else {
queue[rear] = data;
rear++;
}
return ;
}
static void queueDequeue()
{
if (front == rear) {
System.out.printf( "\nQueue is empty\n" );
return ;
}
else {
for ( int i = 0 ; i < rear - 1 ; i++) {
queue[i] = queue[i + 1 ];
}
if (rear < capacity)
queue[rear] = 0 ;
rear--;
}
return ;
}
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf( "\nQueue is Empty\n" );
return ;
}
for (i = front; i < rear; i++) {
System.out.printf( " %d <-- " , queue[i]);
}
return ;
}
static void queueFront()
{
if (front == rear) {
System.out.printf( "\nQueue is Empty\n" );
return ;
}
System.out.printf( "\nFront Element is: %d" ,
queue[front]);
return ;
}
}
public class StaticQueueinjava {
public static void main(String[] args)
{
Queue q = new Queue( 4 );
q.queueDisplay();
q.queueEnqueue( 20 );
q.queueEnqueue( 30 );
q.queueEnqueue( 40 );
q.queueEnqueue( 50 );
q.queueDisplay();
q.queueEnqueue( 60 );
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf(
"\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
}
}
|
Python3
class Queue:
def __init__( self , c):
self .queue = []
self .front = self .rear = 0
self .capacity = c
def queueEnqueue( self , data):
if ( self .capacity = = self .rear):
print ( "\nQueue is full" )
else :
self .queue.append(data)
self .rear + = 1
def queueDequeue( self ):
if ( self .front = = self .rear):
print ( "Queue is empty" )
else :
x = self .queue.pop( 0 )
self .rear - = 1
def queueDisplay( self ):
if ( self .front = = self .rear):
print ( "\nQueue is Empty" )
for i in self .queue:
print (i, "<--" , end = '')
def queueFront( self ):
if ( self .front = = self .rear):
print ( "\nQueue is Empty" )
print ( "\nFront Element is:" ,
self .queue[ self .front])
if __name__ = = '__main__' :
q = Queue( 4 )
q.queueDisplay()
q.queueEnqueue( 20 )
q.queueEnqueue( 30 )
q.queueEnqueue( 40 )
q.queueEnqueue( 50 )
q.queueDisplay()
q.queueEnqueue( 60 )
q.queueDisplay()
q.queueDequeue()
q.queueDequeue()
print ( "\n\nafter two node deletion\n" )
q.queueDisplay()
q.queueFront()
|
C#
using System;
public class Queue {
private int front, rear, capacity;
private int [] queue;
public Queue( int c)
{
front = rear = 0;
capacity = c;
queue = new int [capacity];
}
public void queueEnqueue( int data)
{
if (capacity == rear) {
Console.Write( "\nQueue is full\n" );
return ;
}
else {
queue[rear] = data;
rear++;
}
return ;
}
public void queueDequeue()
{
if (front == rear) {
Console.Write( "\nQueue is empty\n" );
return ;
}
else {
for ( int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return ;
}
public void queueDisplay()
{
int i;
if (front == rear) {
Console.Write( "\nQueue is Empty\n" );
return ;
}
for (i = front; i < rear; i++) {
Console.Write( " {0} <-- " , queue[i]);
}
return ;
}
public void queueFront()
{
if (front == rear) {
Console.Write( "\nQueue is Empty\n" );
return ;
}
Console.Write( "\nFront Element is: {0}" ,
queue[front]);
return ;
}
}
public class StaticQueueinjava {
public static void Main(String[] args)
{
Queue q = new Queue(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
Console.Write( "\n\nafter two node deletion\n\n" );
q.queueDisplay();
q.queueFront();
}
}
|
Javascript
<script>
class Queue{
constructor(c)
{
this .front = this .rear = 0;
this .capacity = c;
this .queue = new Array( this .capacity);
}
queueEnqueue(data)
{
if ( this .capacity == this .rear)
{
document.write( "<br>Queue is full<br>" );
return ;
}
else
{
this .queue[ this .rear] = data;
this .rear++;
}
return ;
}
queueDequeue()
{
if ( this .front == this .rear)
{
document.write( "<br>Queue is empty<br>" );
return ;
}
else
{
for (let i = 0; i < this .rear - 1; i++)
{
this .queue[i] = this .queue[i + 1];
}
if ( this .rear < this .capacity)
this .queue[ this .rear] = 0;
this .rear--;
}
return ;
}
queueDisplay()
{
let i;
if ( this .front == this .rear)
{
document.write( "<br>Queue is Empty<br>" );
return ;
}
for (i = this .front; i < this .rear; i++)
{
document.write( this .queue[i] + " <-- " );
}
return ;
}
queueFront()
{
if ( this .front == this .rear)
{
document.write( "<br>Queue is Empty<br>" );
return ;
}
document.write( "<br>Front Element is: " +
this .queue[ this .front]);
return ;
}
}
let q = new Queue(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
document.write( "<br><br> after two node deletion <br><br>" );
q.queueDisplay();
q.queueFront();
</script>
|
Output
Queue is Empty
20 <-- 30 <-- 40 <-- 50 <--
Queue is full
20 <-- 30 <-- 40 <-- 50 <--
after two node deletion
40 <-- 50 <--
Front Element is: 40
Time Complexity: O(1) for Enqueue (element insertion in the queue) as we simply increment pointer and put value in array and O(N) for Dequeue (element removing from the queue) as we use for loop to implement that.
Auxiliary Space: O(N), as here we are using an N size array for implementing Queue
Optimizations :
We can implement both enqueue and dequeue operations in O(1) time. To achieve this, we can either use Linked List Implementation of Queue or circular array implementation of queue.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Mar, 2023
Like Article
Save Article