Advertisement
dirtydevil123

Assignment 2 (Queue )

Jul 10th, 2021 (edited)
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <string>
  3. #include <unistd.h>
  4. using namespace std;
  5.  
  6. #define MAX 10
  7.  
  8. int itemCount = 0;
  9. int front = 0;
  10. int rear = -1;
  11. string itemArray[10];
  12.  
  13.  
  14.  
  15. bool isEmpty()
  16. {
  17.     return itemCount == 0;
  18. }
  19.  
  20. bool isFull()
  21. {
  22.     return itemCount == MAX;
  23. }
  24.  
  25. int size()
  26. {
  27.     return itemCount;
  28. }
  29.  
  30. void insert(string &personName)
  31. {
  32.     if (!isFull())
  33.     {
  34.         if (rear == MAX - 1)
  35.         {
  36.             rear = -1;
  37.         }
  38.         //cout << "insertName = " << personName << endl;
  39.         itemArray[++rear] = personName;
  40.         itemCount++;
  41.     }
  42. }
  43.  
  44. string removeData()
  45. {
  46.     sleep(2);
  47.     string personName = itemArray[front++];
  48.  
  49.     if (front == MAX)
  50.     {
  51.         front = 0;
  52.     }
  53.  
  54.     itemCount--;
  55.  
  56.     return personName;
  57. }
  58. int main()
  59. {
  60.     int round = 3;
  61.     for (int i = 1; i <= round; i++)
  62.     {
  63.  
  64.         cout << "Please Enter the name of the 10 people for round " << i << endl;
  65.         string name;
  66.  
  67.         for (int i = 0; i < 10; i++)
  68.         {
  69.             cin >> name;
  70.             insert(name);
  71.         }
  72.  
  73.         cout<< "Queue Full! Wait for the next round...\n";
  74.  
  75.         while (!isEmpty())
  76.         {
  77.  
  78.             string showName = removeData();
  79.             cout << showName << " is leaving...\n\n";
  80.         }
  81.     }
  82.     cout << "Bank is closed for the day.\nCome again tomorrow!\nWear mask and stay safe!\n";
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement