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

Cqueue

This document contains a C++ implementation of a circular queue with methods for enqueueing, dequeueing, and displaying elements. The queue has a maximum size defined by the constant MAX and handles overflow and underflow conditions. A menu-driven interface allows users to interact with the queue through a console application.
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)
0 views3 pages

Cqueue

This document contains a C++ implementation of a circular queue with methods for enqueueing, dequeueing, and displaying elements. The queue has a maximum size defined by the constant MAX and handles overflow and underflow conditions. A menu-driven interface allows users to interact with the queue through a console application.
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/ 3

#include <iostream>

#define MAX 5 // Define the maximum size of the circular queue

using namespace std;

class CircularQueue {
private:
int front, rear;
int cqueue[MAX]; // Array to store the queue elements

public:
// Constructor to initialize the queue
CircularQueue() {
front = -1;
rear = -1;
}

// Function to insert an element into the circular queue


void enqueue(int item) {
if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {
cout << "Queue Overflow! Cannot enqueue " << item << "." << endl;
return;
}

if (front == -1) { // If the queue is empty


front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX; // Circular increment of rear
}
cqueue[rear] = item;
cout << "Enqueued: " << item << endl;
}

// Function to remove an element from the circular queue


void dequeue() {
if (front == -1) { // If the queue is empty
cout << "Queue Underflow! No elements to dequeue." << endl;
return;
}

cout << "Dequeued: " << cqueue[front] << endl;


if (front == rear) { // If there is only one element in the queue
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX; // Circular increment of front
}
}
// Function to display the elements of the circular queue
void display() {
if (front == -1) { // If the queue is empty
cout << "Queue is empty!" << endl;
return;
}

cout << "Queue elements: ";


if (front <= rear) {
for (int i = front; i <= rear; i++) {
cout << cqueue[i] << " ";
}
} else {
for (int i = front; i < MAX; i++) {
cout << cqueue[i] << " ";
}
for (int i = 0; i <= rear; i++) {
cout << cqueue[i] << " ";
}
}
cout << endl;
}
};

int main() {
CircularQueue cq; // Create a CircularQueue object
int choice, item;

do {
// Display menu
cout << "\nMenu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter the element to enqueue: ";
cin >> item;
cq.enqueue(item);
break;
case 2:
cq.dequeue();
break;
case 3:
cq.display();
break;
case 4:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 4);

return 0;
}

You might also like