Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //Source by - codewhoop.com / CodeWhoop - Youtube / Edited by Hilmi&Teng
- using namespace std;
- #define SIZE 5
- int circleQueue[SIZE];
- int front{ -1 };
- int rear{ -1 };
- bool ISempty();
- void InsertQueue();
- void Dequeue();
- void ShowFrontRear();
- void DisplayQueue();
- int main()
- {
- //1. We Will do a simple CLI choise.
- int choice{ 0 };
- bool isLooping{ true };
- do
- {
- cout << "\n-----------------------------------------------------\n";
- cout << " 1.Insert Queue\n 2.Dequeue\n 3.ShowFrontRear\n 4.Print All\n 5.End \n ";
- cout << "Choice :";
- cin >> choice;
- if (choice == 1) InsertQueue();
- if (choice == 3) ShowFrontRear();
- if (choice == 2) Dequeue();
- if (choice == 4) DisplayQueue();
- if (choice == 5) isLooping = false;
- } while (true);
- }
- bool ISempty()
- {
- if (front == -1 && rear == -1)
- {
- return true;
- }
- return false;
- }
- void InsertQueue()
- {
- int value{ 0 };
- cout << "Please enter value to be enQueued : ";
- cin >> value;
- //Find if Queue is full
- //1. If the rear index + 1 is equal to front means it's full
- if ((rear + 1) % SIZE == front)
- {
- cout << "Queue is full \n";
- return;
- }
- //if the first element is -1 make it 0;
- if (front == -1)
- front = 0;
- //Move the rear index forward and insert value
- rear = (rear + 1) % SIZE;
- circleQueue[rear] = value;
- }
- void Dequeue()
- {
- // Check if the queue is empty so it will not underflow
- if (ISempty())
- {
- cout << "Queue is empty";
- return;
- }
- //If it's only one value left
- if (front == rear)
- {
- front = -1;
- rear = -1;
- return;
- }
- front = (front + 1) % SIZE;
- }
- void ShowFrontRear()
- {
- if (ISempty())
- {
- cout << "Queue is empty";
- return;
- }
- cout << "\nElement at front is [" << front << "] " << endl;
- cout << "Element at rear is [" << rear << "] " << endl;
- }
- void DisplayQueue()
- {
- if (ISempty())
- {
- cout << "Queue is empty";
- return;
- }
- int i;
- if (front <= rear)
- {
- for (i = front; i <= rear; i++)
- cout << circleQueue[i] << " ";
- }
- else
- {
- i = front;
- while (i < SIZE)
- {
- cout << circleQueue[i] << " ";
- i++;
- }
- i = 0;
- while (i <= rear)
- {
- cout << circleQueue[i] << " ";
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement