
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Queue Using Array
A queue is a linear data structure that contains a collection of elements. Queue implements the FIFO mechanism i.e., the element that is inserted first is also deleted first. In other words, the least recently added element is removed first in a queue.
Implementation Steps
Based on the below implementation. Here are the steps to implement queue using array:
- Initialize Queue: Create an array queue[100] and set front and rear to -1 to indicate an empty queue.
- Predefine Input Values: Store input elements in input_values[] for insertion.
- Insert Operation: Before inserting an element to the queue, check for overflow (rear == n - 1). If queue is initially empty, set front = 0. And, insert next value from input_values[] into the queue and increment rear.
- Delete Operation Before deleting an element from the queue, check for underflow (front == -1 || front > rear). If valid, remove element at front and increment front.
- Display Operation: For the display operation, check if queue is empty, show a message. Otherwise, print elements from front to rear.
- Main Function Execution: In the main() function, perform a sequence operations such as insertion, deletion, and display to demonstrate queue functionality.
C++ Program to Implement Queue using Array
Here is the implementation of queue using array:
#include <iostream> using namespace std; int queue[100], n = 100, front = -1, rear = -1; int input_values[] = {10, 20, 30, 40, 50}; int input_index = 0; int total_inputs = sizeof(input_values) / sizeof(input_values[0]); void Insert() { if (rear == n - 1) { cout << "Queue Overflow" << endl; } else { if (front == -1) front = 0; if (input_index < total_inputs) { int val = input_values[input_index++]; cout << "Inserting element: " << val << endl; rear++; queue[rear] = val; } else { cout << "No more predefined elements to insert." << endl; } } } void Delete() { if (front == -1 || front > rear) { cout << "Queue Underflow" << endl; return; } else { cout << "Element deleted from queue is: " << queue[front] << endl; front++; } } void Display() { if (front == -1 || front > rear) cout << "Queue is empty" << endl; else { cout << "Queue elements are: "; for (int i = front; i <= rear; i++) cout << queue[i] << " "; cout << endl; } } int main() { Insert(); Insert(); Display(); Delete(); Display(); Insert(); Insert(); Insert(); Insert(); Display(); return 0; }
The output of the above program is as follows ?
Inserting element: 10 Inserting element: 20 Queue elements are: 10 20 Element deleted from queue is: 10 Queue elements are: 20 Inserting element: 30 Inserting element: 40 Inserting element: 50 No more predefined elements to insert. Queue elements are: 20 30 40 50
Code Explanation
In the above program, the 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. This is shown below ?
void Insert() { if (rear == n - 1) { cout << "Queue Overflow" << endl; } else { if (front == -1) front = 0; if (input_index < total_inputs) { int val = input_values[input_index++]; cout << "Inserting element: " << val << endl; rear++; queue[rear] = val; } else { cout << "No more predefined elements to insert." << endl; } } }
In the 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. This is shown below ?
void Delete() { if (front == -1 || front > rear) { cout << "Queue Underflow" << endl; return; } else { cout << "Element deleted from queue is: " << queue[front] << endl; front++; } }
In the function Display(), if front is -1 then queue is empty. Otherwise all the queue elements are displayed using a for loop. This is shown below ?
void Display() { if (front == -1 || front > rear) cout << "Queue is empty" << endl; else { cout << "Queue elements are: "; for (int i = front; i <= rear; i++) cout << queue[i] << " "; cout << endl; } }