Advertisement
enkacang

Presentation 3PM

Aug 12th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. //Source by - codewhoop.com / CodeWhoop - Youtube / Edited by Hilmi&Teng
  3.  
  4. using namespace std;
  5.  
  6. #define SIZE 5
  7. int circleQueue[SIZE];
  8. int front{ -1 };
  9. int rear{ -1 };
  10.  
  11. bool ISempty();
  12. void InsertQueue();
  13. void Dequeue();
  14. void ShowFrontRear();
  15. void DisplayQueue();
  16.  
  17. int main()
  18. {
  19.     //1. We Will do a simple CLI choise.
  20.     int choice{ 0 };
  21.     bool isLooping{ true };
  22.     do
  23.     {
  24.         cout << "\n-----------------------------------------------------\n";
  25.         cout << " 1.Insert Queue\n 2.Dequeue\n 3.ShowFrontRear\n 4.Print All\n 5.End \n ";
  26.         cout << "Choice :";
  27.         cin >> choice;
  28.  
  29.         if (choice == 1) InsertQueue();
  30.         if (choice == 3) ShowFrontRear();
  31.         if (choice == 2) Dequeue();
  32.         if (choice == 4) DisplayQueue();
  33.         if (choice == 5) isLooping = false;
  34.  
  35.     } while (true);
  36.  
  37. }
  38.  
  39. bool ISempty()
  40. {
  41.     if (front == -1 && rear == -1)
  42.     {
  43.         return true;
  44.     }
  45.     return false;
  46. }
  47.  
  48. void InsertQueue()
  49. {
  50.     int value{ 0 };
  51.     cout << "Please enter value to be enQueued : ";
  52.     cin >> value;
  53.  
  54.     //Find if Queue is full
  55.     //1. If the rear index + 1 is equal to front means it's full
  56.     if ((rear + 1) % SIZE == front)
  57.     {
  58.         cout << "Queue is full \n";
  59.         return;
  60.     }
  61.  
  62.     //if the first element is -1 make it 0;
  63.     if (front == -1)
  64.         front = 0;
  65.     //Move the rear index forward and insert value
  66.     rear = (rear + 1) % SIZE;
  67.     circleQueue[rear] = value;
  68. }
  69.  
  70. void Dequeue()
  71. {
  72.     // Check if the queue is empty so it will not underflow
  73.     if (ISempty())
  74.     {
  75.         cout << "Queue is empty";
  76.         return;
  77.     }
  78.  
  79.     //If it's only one value left
  80.     if (front == rear)
  81.     {
  82.         front = -1;
  83.         rear = -1;
  84.         return;
  85.     }
  86.  
  87.     front = (front + 1) % SIZE;
  88. }
  89.  
  90. void ShowFrontRear()
  91. {
  92.     if (ISempty())
  93.     {
  94.         cout << "Queue is empty";
  95.         return;
  96.     }
  97.  
  98.     cout << "\nElement at front is [" << front << "] " << endl;
  99.     cout << "Element at rear is [" << rear << "] " << endl;
  100. }
  101.  
  102. void DisplayQueue()
  103. {
  104.     if (ISempty())
  105.     {
  106.         cout << "Queue is empty";
  107.         return;
  108.     }
  109.     int i;
  110.     if (front <= rear)
  111.     {
  112.         for (i = front; i <= rear; i++)
  113.             cout << circleQueue[i] << " ";
  114.     }
  115.     else
  116.     {
  117.         i = front;
  118.         while (i < SIZE)
  119.         {
  120.             cout << circleQueue[i] << " ";
  121.             i++;
  122.         }
  123.         i = 0;
  124.         while (i <= rear)
  125.         {
  126.             cout << circleQueue[i] << " ";
  127.             i++;
  128.         }
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement