Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 %= 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement