How to implement an Queue using an Array?

This article demonstrates how to implement an Queue using an Array. Each operation is successfully written in C++ with the help of functions.

Function Insert()

Inserts an element into the queue. If the rear is equal to n-1, then the queue is full, and overflow is displayed. If front is -1, it is incremented by 1. Then rear is incremented by 1 and the element is inserted in index of rear.

Code for Insertion

void Insert() {
  int val;
  if (rear == n - 1)
  cout<<"Queue Overflow"<<endl;

  else {
     if (front == - 1)
     front = 0;
     cout<<"Insert the element in queue : "<<endl;
     cin>>val;
     rear++;
     queue[rear] = val;
  }
}

Function Delete(),

If there are no elements in the queue then it is underflow condition. Otherwise, the element at front is displayed and front is incremented by one.

Code for Deletion

void Delete() {
  if (front == - 1 || front > rear) {
     cout<<"Queue Underflow ";
     return ;
  }
  else {
     cout<<"Element deleted from queue is : "<< queue[front] <<endl;
     front++;;
  }
}

Function display(),
if front is -1 then queue is empty. Otherwise, all the queue elements are displayed using a for loop

Code to display the queue:

void Display() {
   if (front == - 1)
   cout<<"Queue is empty"<<endl;
   else {
      cout<<"Queue elements are : ";
      for (int i = front; i <= rear; i++)
      cout<<queue[i]<<" ";
      cout<<endl;
   }
}