0% found this document useful (0 votes)
13 views

QUEUES-ADT(programs)

The document provides a C++ implementation of a queue using an array, detailing the basic operations such as enqueue, dequeue, and display. It also explains the concept of circular queues, their advantages over linear queues, and includes a separate implementation for circular queues with operations to check if the queue is empty or full. The document includes code examples for both standard and circular queue implementations.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

QUEUES-ADT(programs)

The document provides a C++ implementation of a queue using an array, detailing the basic operations such as enqueue, dequeue, and display. It also explains the concept of circular queues, their advantages over linear queues, and includes a separate implementation for circular queues with operations to check if the queue is empty or full. The document includes code examples for both standard and circular queue implementations.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

++ Program to Implement Queue using Array

The following program demonstrates how we can implement a queue using array in
C++:
// C++ Program to implement a queue using array

#include <iostream>

using namespace std;

// defining the max size of the queue

#define MAX_SIZE 100

// Implement the queue data structure

class Queue {

public:

int front;

int rear;

int arr[MAX_SIZE];

// initializing pointers in the constructor

Queue(): front(-1), rear(-1) {}

// Function to check if the queue is empty or not

bool isEmpty() { return front == -1 || front > rear; }

// Function to check if the queue is full or not

bool isFull() { return rear == MAX_SIZE - 1; }

// Function to get the front element of the queue

int getFront()

{
if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

return arr[front];

// Function to get the rear element of the queue

int getRear()

if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

return arr[rear];

// Function to enqueue elements from the queue

void enqueue(int val)

{
// 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;

// Function to dequeue elements from the queue

int dequeue()

{
// Check underflow condition

if (isEmpty()) {

cout << "Queue is empty" << endl;

return -1;

int ans = arr[front];

front++;

// if queue becomes empty, reset both pointers

if (isEmpty())

front = rear = -1;

return ans;

// Display function to print the queue


void display()

if (isEmpty()) {

cout << "Queue is empty" << endl;

return;

}
cout << "Queue: ";

for (int i = front; i <= rear; i++) {

cout << arr[i] << " ";

cout << endl;

};

int main()

{
// Created Queue of size 5

Queue q;

// Enqueueing elements

q.enqueue(1);

q.enqueue(2);

q.enqueue(3);

// Displaying status of the queue after enqueuing

cout << "\nAfter Enqueueing:" << endl;

cout << "Front element: " << q.getFront() << endl;

cout << "Rear element: " << q.getRear() << endl;

q.display();

// Enqueueing more elements

q.enqueue(4);
q.enqueue(5);

// Displaying the updated queue

q.display();

// Enqueueing one more element to demonstrate overflow


// condition

q.enqueue(6);

// Dequeueing elements

cout << "\nDequeueing elements:" << endl;

cout << "Dequeued element: " << q.dequeue() << endl;

cout << "Dequeued element: " << q.dequeue() << endl;

// Displaying status of the queue after dequeueing

cout << "\nAfter Dequeueing:" << endl;

cout << "Front element: " << q.getFront() << endl;

cout << "Rear element: " << q.getRear() << endl;

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.

What is Circular Queue in C++?


In a circular queue is a queue where the last element of the queue is connected to the
first element of the queue. As it is a queue, it follows the FIFO (First-In-First-Out)
order of operation. It is also known as "Ring buffer" as the resulted data structure is
logically circular.
Consider a scenario where you're simulating a printer queue. New documents are
constantly being added to the queue, and as they are printed, they are removed. In a
circular queue, if the queue becomes full and documents are printed, new documents
can still be added at the beginning of the queue, as it is circular in nature.
Why we need Circular Queue in C++?
Circular queue concept is especially helpful when the queue is implemented using an
array. In linear implementation, when the rear pointer reaches the end of the queue,
we cannot insert new elements even if there are empty locations in the queue.
Circular queues address this limitation by connecting the front and rear ends,
allowing the rear to go back to the start in when the rear reaches the end of the
queue.
Operations on Circular Queue in C++
The basic operations of the circular queue are same as that of normal queue
but the implementation is different. The following table list the basic
operations in circular queue:
Operations on circular queue :
• front : Get the front item from the queue.
• rear : Get the last item from the queue.
• enQueue() : This function is used to enqueue the element into the circular
queue. New element will be added at the rear end of the queue.
• deQueue() : This function is used to delete the element from the circular
queue. Delete operation is done from the front end of the queue.

Representation of Circular Queue in C++


The circular queue in C++ can be represented as a class which contains the rear and
front index pointers, array to store queue elements, and the member functions to
provide the basic operations.

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

Implementation of Dequeue for Circular Queue


The elements are removed from the queue at the front of the queue. We first
need to check whether queue is empty or not.
Algorithm of Dequeue
if (isEmpty(queue)) {
print "Queue Underflow"
return
else
front = (front + 1) % size

Why Circular Queues?


Once the queue becomes full, we cannot insert more elements. Even though
we dequeue few of the elements.
Because the following code –
//Overflow check condition
if (rear == SIZE-1)
cout<<"Overflow condition";

• 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

// defining the max size of the queue

#define MAX_SIZE 5

using namespace std;


// class that represents queue

class Queue {

public:
// index pointers and data array

int front, rear;

int arr[MAX_SIZE];

// constructor to initialize the index pointers

Queue() { front = rear = 0; }

// checking if queue is empty

bool isEmpty()
{

if (front == rear)

return true;

return false;
}

// checking if the queue is full

bool isFull()

{
if ((rear + 1) % MAX_SIZE == front)

return true;

return false;
}

// enqueue operation

void enqueue(int val)

{
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];

// utility to print queue

void print()

{
if (this->isEmpty())

return;

for (int i = (front + 1) % MAX_SIZE; i < rear;

i = (i + 1) % MAX_SIZE) {

printf("%d ", arr[i]);

cout << arr[rear];

};
// 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;

You might also like