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

Corrected Linear Queue

This document contains a C++ implementation of a linear queue with basic operations such as enqueue, dequeue, and display. The queue has a fixed size defined by MAXSIZE and includes error handling for 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)
20 views2 pages

Corrected Linear Queue

This document contains a C++ implementation of a linear queue with basic operations such as enqueue, dequeue, and display. The queue has a fixed size defined by MAXSIZE and includes error handling for 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/ 2

#include <iostream>

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

using namespace std;

class LinearQueue {
private:
int front, rear;
int queue[MAXSIZE]; // Array to store the queue elements

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

// Function to insert an element into the linear queue


void enqueue(int item) {
if (rear == MAXSIZE - 1) { // Check if the 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 linear queue (with shift logic)
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

// Shift remaining elements to the front


if (front > 0) {
for (int i = front; i <= rear; i++) {
queue[i - front] = queue[i];
}
rear = rear - front; // Adjust rear
front = 0; // Reset front to 0
}
}
}

// Function to display the elements of the linear 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() {
LinearQueue lq; // Create a LinearQueue 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;
lq.enqueue(item);
break;
case 2:
lq.dequeue();
break;
case 3:
lq.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