0% found this document useful (0 votes)
12 views2 pages

Lqueue

The document contains a C++ implementation of a fixed-size queue using an array. It includes methods for enqueueing, dequeueing, and displaying the queue elements, along with a simple menu-driven interface for user interaction. The queue has a maximum size defined by the constant MAXSIZE, and it handles overflow and underflow conditions.
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)
12 views2 pages

Lqueue

The document contains a C++ implementation of a fixed-size queue using an array. It includes methods for enqueueing, dequeueing, and displaying the queue elements, along with a simple menu-driven interface for user interaction. The queue has a maximum size defined by the constant MAXSIZE, and it handles overflow and underflow conditions.
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

#include <iostream>

#define MAXSIZE 5 // Define a fixed size for the queue

using namespace std;

class Queue {
private:
int front, rear;
int queue[MAXSIZE]; // Fixed-size array for the queue

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

// Function to add an element to the queue


void enqueue(int item) {
if (rear == MAXSIZE - 1) { // Check if queue is full
cout << "Queue Overflow! Cannot enqueue " << item << "." << endl;
} else {
rear++; // Increment rear
queue[rear] = item; // Add the item to the queue
cout << "Enqueued: " << item << endl;
}
}

// Function to remove an element from the queue


void dequeue() {
if (front > rear) { // Check if the queue is empty
cout << "Queue Underflow! No elements to dequeue." << endl;
} else {
cout << "Dequeued: " << queue[front] << endl;
front++; // Increment front
}
}

// Function to display the elements of the queue


void display() {
if (front > rear) { // Check if the queue is empty
cout << "Queue is empty!" << endl;
} else {
cout << "Queue elements: ";
for (int i = front; i <= rear; i++) {
cout << queue[i] << " ";
}
cout << endl;
}
}
};

int main() {
Queue q; // Create a queue 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;
[Link](item);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
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