Queue using Array
Archived from my old blog, mikirinkode.online (2021–2022). Posted as it was written.
Hi all, this time we will try to learn Queue implementation with Array. What is Queue? Queue is a data structure that contains a collection of data. Queue applies FIFO (First In First Out) where the first data that enter will be the first to leave. For more details, we have discussed it previously here. Now we focus on the implementation.
- Queue using Array
- Queue using Linked List
Queue using Array Illustration
Initial State of Array

So we have an array that can hold 10 elements, which only contains 6 elements. The element at index 0 or the first index is 7.
Dequeue Operation

To dequeue the array, it can be done by removing the value in the front or 0th index, then shifting the next value. In the illustration, the value at 0th index is 7.
Enqueue Operation

If we want to enqueue an array, we need to find the index where the value of the last element is. In the illustration, the last filled index is the 4th index, so we enter the new value 13 to index 5.
Implementation in C++
Source Code
// Implementation of Queue using Array
#include <iostream>
using namespace std;
// Array than can contain 5 elements
const int arraySize = 5;
int front = 0, rear = 0;
string queue[arraySize];
bool isFullArray();
bool isEmptyArray();
void enqueueArray(string data);
void dequeueArray();
int countArray();
void display();
/*
* Main Function
*/
int main(){
cout << "Number of queued: " << countArray() << endl;
enqueueArray("Ezio");
display();
cout << endl;
enqueueArray("CJ");
enqueueArray("BigSmoke");
display();
cout << endl;
dequeueArray();
display();
cout << endl;
cout << "Number of queued: " << countArray() << endl;
return 0;
}
/*
* Queue Operation
*/
bool isFullArray(){
// to check if the rear index value has reached limit
// if true, then the queue is full
if(rear == arraySize){
return true;
} else{
return false;
}
}
bool isEmptyArray(){
// check if the rear index value == 0
// if true, then the queue is still empty
if(rear == 0){
return true;
} else{
return false;
}
}
// enqueue function
void enqueueArray(string data){
if(isFullArray()){
cout << "Queue is full." << endl;
} else{
if(isEmptyArray()){
// if the queue is still empty
// then insert the new data at 0th index
queue[0] = data;
front++;
rear++;
} else {
// if queue is not empty
// then the new value will be put at the last index (rear)
rear++;
queue[rear] = data;
}
cout << data << " successfully added to Queue." << endl;
}
}
// dequeue function
void dequeueArray(){
if(isEmptyArray()){
cout << "Queue is empty." << endl;
} else{
string removedData = queue[front];
// shift each element
// element at 1th index will shift to 0th index and so on
for(int i = 0; i < rear; i++){
queue[i] = queue[i+1];
}
// decrement rear, because the value has shifted
rear--;
cout << removedData << " successfully removed from Queue." << endl;
}
}
// function to calculate amount data in queue
int countArray(){
if(isEmptyArray()){
return 0;
} else if (isFullArray()){
return arraySize;
} else{
return rear;
}
}
// function to print each element in queue
void display(){
cout << "Display queue data." << endl;
if(isEmptyArray()){
cout << "Queue is Empty " << endl;
} else{
for(int i =0; i < arraySize; i++){
if(queue[i] != ""){
cout << i+1 << ". " << queue[i] << endl;
} else{
cout << i+1 << ". Empty" << endl;
}
}
}
}Output
Number of queued: 0
Ezio successfully added to Queue.
Display queue data.
1. Ezio
2. Empty
3. Empty
4. Empty
5. Empty
CJ successfully added to Queue.
BigSmoke successfully added to Queue.
Display queue data.
1. Ezio
2. Empty
3. CJ
4. BigSmoke
5. Empty
successfully removed from Queue.
Display queue data.
1. Empty
2. CJ
3. BigSmoke
4. BigSmoke
5. Empty
Number of queued: 2Conclustion
Alright guys so we have implemented Queue using Array in C++ language. Next we will try to implement it using a Linked List. Thank you for reading.
Keep write code and have a nice day!