0% found this document useful (0 votes)
2 views3 pages

lab_assignment_3

The document presents a laboratory assignment on implementing a Circular Queue in C++. It includes the code for a CircularQueue class with methods for enqueueing, dequeueing, checking if the queue is empty or full, and displaying the queue elements. The main function demonstrates the usage of the CircularQueue class with a series of enqueue and dequeue operations.

Uploaded by

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

lab_assignment_3

The document presents a laboratory assignment on implementing a Circular Queue in C++. It includes the code for a CircularQueue class with methods for enqueueing, dequeueing, checking if the queue is empty or full, and displaying the queue elements. The main function demonstrates the usage of the CircularQueue class with a series of enqueue and dequeue operations.

Uploaded by

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

Laboratory Assignment 1 : Queue

Name : Arjun Deshmukh


Class : SY AIML
Batch : S1
Roll N : 23513

Code :

#include<iostream>

class CircularQueue {
private:
int front, rear;
int max;
int size;
int* queueArray;
bool isFull;

public:
CircularQueue(int maxSize) {
max = maxSize;
size = 0;
front = -1;
rear = -1;
queueArray = new int[max];
isFull = false;
}

~CircularQueue() {
delete[] queueArray;
}

bool is_empty() {
return size == 0;
}

bool is_full() {
return isFull;
}

void enqueue(int element) {


if (!isFull) {
rear = (rear + 1) % max; // Circular increment
queueArray[rear] = element;
size++;
if (size == max) {
isFull = true; // Queue is full when size reaches max
}
std::cout << "Enqueued: " << element << std::endl;
} else {
std::cout << "Queue is full. Cannot enqueue " << element << std::endl;
}
}

void dequeue() {
if (!is_empty()) {
front = (front + 1) % max; // Circular increment
int element = queueArray[front];
size--;
isFull = false; // Reset the flag since an element is dequeued
std::cout << "Dequeued: " << element << std::endl;
} else {
std::cout << "Queue is empty. Cannot dequeue." << std::endl;
}
}

void display() {
if (!is_empty()) {
std::cout << "Elements of the circular queue: ";
int i = front;
do{
i = (i + 1) % max; // Circular increment
std::cout << queueArray[i] << " ";
}
while (i != rear);
std::cout << std::endl;
} else {
std::cout << "Queue is empty." << std::endl;
}
}
};

int main() {
CircularQueue cq(3); // Create a circular queue of size 3

cq.enqueue(12);
cq.enqueue(3);
cq.enqueue(4);
cq.dequeue();
cq.display();
cq.enqueue(9);
cq.display();
cq.enqueue(10);
cq.display();

return 0;
}
Output :

You might also like