QUEUES-ADT(programs)
QUEUES-ADT(programs)
The following program demonstrates how we can implement a queue using array in
C++:
// C++ Program to implement a queue using array
#include <iostream>
class Queue {
public:
int front;
int rear;
int arr[MAX_SIZE];
int getFront()
{
if (isEmpty()) {
return -1;
return arr[front];
int getRear()
if (isEmpty()) {
return -1;
return arr[rear];
{
// Check overflow condition
if (isFull()) {
cout << "Queue is full" << endl;
return;
}
// if queue is empty, set front to 0
if (isEmpty())
front = 0;
rear++;
arr[rear] = val;
int dequeue()
{
// Check underflow condition
if (isEmpty()) {
return -1;
front++;
if (isEmpty())
return ans;
if (isEmpty()) {
return;
}
cout << "Queue: ";
};
int main()
{
// Created Queue of size 5
Queue q;
// Enqueueing elements
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.display();
q.enqueue(4);
q.enqueue(5);
q.display();
q.enqueue(6);
// Dequeueing elements
q.display();
return 0;
}
Output
After Enqueueing:
Front element: 1
Rear element: 3
Queue: 1 2 3
Queue: 1 2 3 4 5
Dequeueing elements:
Dequeued element: 1
Dequeued element: 2
After Dequeueing:
Front element: 3
Rear element: 6
Queue: 3 4 5 6
Circular Queues:
In C++, Queues are a fundamental data structure in computer science which
works on the principle of FIFO (First In, First Out). They can be implemented
using both array and linked list. A circular queue is a type of queue in which
the last element is connected to the first element, forming a circular structure.
class Queue {
private:
int front, rear;
int arr[MAX_SIZE];
public
........
}
Implementation of isEmpty for Circular Queue
The queue is only empty when the front == rear pointer
Algorithm of isEmpty
if front is equal to rear
return true
else
return false
Implementation of isFull for Circular Queue
The queue will be full when the incremented rear pointer is equal to the front
pointer.
Algorithm of isFull
if (rear + 1) % size is equal to front
return true;
else
return false;
Implementation of Enqueue for Circular Queue
We insert the new elements at the end of the queue using rear pointer. We first
check if the queue is full or not.
Algorithm of Enqueue
if (isFull(queue)) {
print "Queue Overflow"
return
else
rear = (rear + 1) % size
arr[rear] = new_element
• After each enqueue, rear value is incremented & once queue is full rear
value becomes equal to size – 1
//Enqueue function
void Enqueue(){
rear++;
}
//Dequeue Function
void Dequeue(){
front--;
}
• So no more elements can be entered & even if dequeue happens, as it
only changes the front value & not rear value.
Circular Ques:
C++ program to implement the circular queue using array
#include <bits/stdc++.h> //includes every standard library
#define MAX_SIZE 5
class Queue {
public:
// index pointers and data array
int arr[MAX_SIZE];
bool isEmpty()
{
if (front == rear)
return true;
return false;
}
bool isFull()
{
if ((rear + 1) % MAX_SIZE == front)
return true;
return false;
}
// enqueue operation
{
if (this->isFull()) {
printf("Queue Overflow!\n");
return;
}
rear = (rear + 1) % MAX_SIZE;
arr[rear] = val;
// dequeue operation
void dequeue()
{
if (this->isEmpty()) {
printf("Queue Underflow!\n");
return;
}
front = (front + 1) % MAX_SIZE;
// peek function
int peek()
{
if (this->isEmpty()) {
printf("Queue is Empty!\n");
return -1;
}
return arr[(front + 1) % MAX_SIZE];
void print()
{
if (this->isEmpty())
return;
i = (i + 1) % MAX_SIZE) {
};
// driver code
int main()
{
Queue q;
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.enqueue(11);
q.dequeue();
q.dequeue();
q.enqueue(123);
q.print();
return 0;