Queue Implementation using Array

Queue is also a collection of homogeneous data elements like array , array can be used to to implement the queue . 

To implement the queue from an array ,we have to declare the array named queue[Max] , where Max is the size  of array . 
As in queue insertion and deletion take place at two different ends , we need two variables Front and Rear to keep track of queue .
Graphical representation of the Queue is shown in the following diagram .

Simple representation of queue using array







When Rear = Front = -1 , indicate that queue is empty  .
Queue usually grows at  Rear end and shrinks at Front end .

 Operations on Queue
Is_Empty() :  Is_Empty() is a function which is used to check whether queue is empty or not . 
It returns true if queue is empty else returns false . 
Code for Is_Empty () :
bool Is_Empty()
{
    if (Front == -1)
        return true;
    else
        return false;
  
}


Is_Full() : Is_Full() is used to check whether queue is empty or not .  
It returns true if queue is full else returns false . 

Code for Is_Empty () :

bool Is_Full()
{
    if ( Rear ==(Max-1) )
        return true;
    else
        return false;

}

Enqueue(int value)  : 
Enqueue(int value) is used to insert data element at the Rear end of the queue. 
The value of rear is incremented by 1 and then the data element is placed at that Rear index . 

Let's understand the Enqueue operation with the help of an example .
Suppose we have to create a queue with three elements . To create it , we need to call Enqueue function three times .
Initially when queue is empty , Rear = Front = -1 . 
Null Queue
Null Queue
When Enqueue(A)  is called , both Rear and Front are incremented by 1 . Now , Rear = Front = 0 . 
insert element in queue
Again when Enqueue(B)  is called ,  Rear is incremented by 1 . Now , Rear = 1  and  Front = 0 . 
Insert element in Queue
 When Enqueue(C)  is called ,  Rear is incremented by 1 . Now , Rear = 2  and  Front = 0 . 
Insert element in queue

The queue with three elements looks like the diagram shown above .  

Code for Enqueue() :
void enqueue(int value)
{
    if ( Front == -1 && Rear == -1 )
    {
        Front = 0;
        Rear = 0;
    }
    else
    {
        Rear = Rear + 1;
    }
    queue[Rear] = value ;
}

Dequeue() :   Dequeue is used to delete the data element at the front of queue . 
Whenever Dequeue function is called , each time Front is incremented by 1 .
Suppose , we have a queue above queue () as an example .We  have to dequeue all the elements from it . 
Initially  Front = 0 , Rear = 2

When dequeue() is called , Front = 1 , Rear = 2.

Dequeue
 When dequeue() is called again , Front = 2 , Rear = 2.
Dequeue elements from queue 
When dequeue() is called , Front = -1 , Rear = -1 . 


Dequeue from queue

Code for Dequeue()


int dequeue()
{
    int x = queue[Front];

    if(Front==Rear)
    {
        Front = -1;
        Rear = -1;

    }
    else
        Front = Front + 1;
    return x;
}
    
# C++ Program for Queue using Array  

Download the code here .
#include<iostream>

using namespace std;

#define Max  10

int  queue[Max];

int  Front , Rear ;

bool  Is_Empty();

bool  IsFull();

void  enqueue(int  value);

int  dequeue();

void  display();


int main()

{
    Front = Rear = -1;

    int data , option;

    cout<<"Queue Implementation using Array \n";

    do
    {
        cout<<"---------------------------------------------------------\n";

        cout<<"\t\t  1.Enqueue\n";

        cout<<"\t\t   2.Dequeue\n";

        cout<<"\t\t  3.Display\n";

        cout<<"\t\t  4.Check whether queue is empty \n";

        cout<<"\t\t  5.Check whether queue is full \n";

        cout<<"\t\t  6.Exit\n";

        cout<<"\tYour choise  : ";

       cin>>option;

    switch(option)
    {
        case 1:

        if(!IsFull())
        {
            cout<<"Enter the data to enqueue  : ";
            cin>>data;

            enqueue(data);

        }
        else

            cout<<"Cannot inseret additional element in queue ! Queue is Full\n";

        break;

        case 2:

            if(Is_Empty())

                cout<<"Cannot delete the element ! Queue is Empty !\n";

            else

                cout<<"Deleted element is : "<<dequeue()<<endl;

        break;

        case 3:

             if(Is_Empty() )

                        cout<<"Oops no elements to display \n";

           else
             {
                      cout<<"Displaying the elements of queue  \n";

                      display();

             }

        break;

        case 4:

            if(Is_Empty())

                cout<<"Queue is empty\n";

            else

                cout<<"Queue is not empty \n ";

        break;

        case 5:
            if(IsFull())

                cout<<"Queue is Full ! \n ";

            else

                cout<<"Queue is not Full \n ";

        break;

    }

    }while(option<=5 && option>=1);

}

bool Is_Empty()
{
    if (Front == -1)

        return true;

    else

        return false;
   
}

bool IsFull()
{
    if(Rear ==(Max-1))

        return true;

    else

        return false;

}

void enqueue(int value)
{
    if (Front == -1 && Rear == -1)
    {
        Front = 0;

        Rear = 0;
    }
    else
    {
        Rear = Rear + 1;
    }
    queue[Rear] = value ;
}

int dequeue()
{
    int x = queue[Front];

     if (Front==Rear)

    {
        Front = -1;

        Rear = -1;

    }
  else
        Front = Front + 1;

    return x;
}

void display()
{
    int temp = Front;

    while(temp<=Rear)

    {
        cout<<"\t\t"<<queue[temp]<<endl;

        temp =temp +1;
    }
}




Note :  This code is successfully compiled and run under Linux distribution Ubuntu 18.04 LTS environment .   

Output :

Queue Implementation using Array
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 1
Enter the data to enqueue  : 10
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 1
Enter the data to enqueue  : 20
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 1
Enter the data to enqueue  : 30
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 3
Displaying the elements of queue 
        10
        20
        30
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 4
Queue is not empty
 ---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 5
Queue is not Full
 ---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 2
Deleted element is : 10
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 2
Deleted element is : 20
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 2
Deleted element is : 30
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 4
Queue is empty
---------------------------------------------------------
          1.Enqueue
          2.Dequeue
          3.Display
          4.Check whether queue is empty
          5.Check whether queue is full
          6.Exit
    Your choise  : 2
Cannot delete the element ! Queue is Empty !

Advantages of Queue using Array
  1. Easy to implement.
  2. Fast execution .
  3. No need to concern about pointer and memory address .
Disadvantages of Queue using Array
  1. Size of an array needs to be declared in advance , which is not possible at all time .
  2. Size of array cannot be altered during the execution .
  3. Needs continuous  memory  locations .
Conclusion 
Queue can be easily implemented using array .