#include <iostream>
#define MAX 5
using namespace std;
int Q[MAX];
int head = -1;
int tail = -1;
int count = 0;
bool isEmpty() {
if (count == 0) return true;
else return false;
}
bool isFull() {
if (count == MAX) return true;
else return false;
}
void display() {
if (count) {
for (int i = 0, j = head; i < count; i++, ++j %= MAX)
cout << Q[j] << " ";
cout << endl;
}
else cout << "Queue is Empty" << endl;
}
void enqueue(int x) {
if (isFull()) {
cout << "Cannot Enqueue " << x << endl;
cout << "Queue is Full/OverFlow" << endl;
}
else {
Q[++tail % MAX] = x;
if (isEmpty()) head++;
count++;
}
display();
}
void dequeue() {
if (count) {
head++;
head %= MAX;
count--;
if (isEmpty()) {
head = -1;
tail = -1;
}
display();
}
else {
cout << "Cannot Dequeue" << endl;
cout << "Queue is Empty/Underflow" << endl;
}
}
int main() {
int choice, value;
while (true) {
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display" << endl;
cout << "4. Quit" << endl;
cout << "Choice: ";
cin >> choice;
if (choice == 1) {
cout << "Enter value to Enqueue: ";
cin >> value;
enqueue(value);
}
else if (choice == 2) dequeue();
else if (choice == 3) display();
else if (choice == 4) break;
else cout << "Invalid Choice" << endl;
cout << endl;
}
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.