Array implementation of queue in C++


A queue is a linear data structure in which the order of operation is FIFO (first in first out).

The array is a data structure that contains elements of the same data type, stored in continuous memory location.

In queue the insertion and deletion operations as done at opposite ends of the queue. The implementation is a bit more complex as compared to the stack.

In array implementation of queue, we create an array queue of size n with two variables top and end.

Now, initially, the array is empty i.e. both top and end are at 0 indexes of the array. And as elements are added to the queue (insertion) the end variable's value is increased. The value of the end can increase up to n i.e. max length of an array.

When elements are to be deleted from the queue (deletion), the top variable’s value is increased. The value of top can go up to the value of the end.

Implementation of queue operation

Enqueue − It is the operation for the addition of elements to the queue. Before adding elements to queue, we will check if the queue is full or not. Condition to check it ends, if it’s less than n then we can store elements at queue[end]. And increase end by 1.

An overflow condition is when the array is full i.e. end == n.

Dequeue − It is the operation of deleting elements of the queue. Before deleting the elements of the queue, we will check if the queue is empty or not. Condition to check if the queue is empty or not, the values of top and end are checked. If top == end than the array is empty.

If there are elements then we will dequeue the array. By shifting all the elements on the left of the array by one.

Front − extracting the first element of the queue i.e. array[top]. This operation can only be performed when the array is not empty.

Display − This operation displays all the elements of the queue. I.e traverses the queue.

Algorithm

ENQUEUE :
Step 1 : if (end == n), print : “OVERFLOW”. Exit
Step 2 : queue[end] = data and end++
DEQUEUE :
Step 1 : If (top == 0), print : “empty Queue”. Exit
Step 2 : shift all elements one position left. End-- ;

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Queue {
   int top, end, n;
   int* queue;
   Queue(int c){
      top = end = 0;
      n = c;
      queue = new int;
   }
   ~Queue() { delete[] queue;
}
void Enqueue(int data){
   if (n == end) {
      printf("\nQueue is full\n");
      return;
   }
   else {
      queue[end] = data;
      end++;
   }
   return;
}
void Dequeue(){
   if (top == end) {
      printf("\nQueue is empty\n");
      return;
   }
   else {
      for (int i = 0; i < end - 1; i++) {
         queue[i] = queue[i + 1];
      }
      end--;
   }
   return;
}
void Display(){
   int i;
   if (top == end) {
      printf("\nQueue is Empty\n");
      return;
   }
   for (i = top; i < end; i++) {
      printf(" %d <-- ", queue[i]);
   }
   return;
}
void Front(){
   if (top == end) {
      printf("\nQueue is Empty\n");
      return;
   }
   printf("\nFront Element is: %d", queue[top]);
   return;
}
};
int main(void){
   Queue q(4);
   q.Display();
   q.Enqueue(12);
   q.Enqueue(89);
   q.Enqueue(65);
   q.Enqueue(34);
   q.Display();
   q.Enqueue(92);
   q.Display();
   q.Dequeue();
   q.Dequeue();
   q.Display();
   q.Front();
   return 0;
}

Output

Queue is Empty
12 <-- 89 <-- 65 <-- 34 <--
Queue is full
12 <-- 89 <-- 65 <-- 34 <-- 65 <-- 34 <--
Front Element is: 65

Updated on: 24-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements