sidrs

FDS (LV) Ass 5 - Queue (Pizza)

Sep 12th, 2024
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class DS {
  6. private:
  7.     int head, tail;
  8.     int size, count;
  9.     string *arr;
  10. public:
  11.     DS(int size) {
  12.         this->size = size;
  13.         head = 0;
  14.         tail = -1;
  15.         arr = new string[size];
  16.     }
  17.  
  18.     bool isFull() {
  19.         return count == size;
  20.     }
  21.  
  22.     bool isEmpty() {
  23.         return count == 0;
  24.     }
  25.  
  26.     void enQueue(string order) {
  27.         if(isFull()){
  28.             cout << "Queue is Full! Cannot Enqueue." << endl;
  29.             return;
  30.         }
  31.         //arr[++tail] = order;
  32.         tail = (tail + 1) % size;
  33.         arr[tail] = order;
  34.         count++;
  35.         cout << "\nOrder: " << order << " has been placed." << endl;
  36.     }
  37.  
  38.     void deQueue() {
  39.         if(isEmpty()){
  40.             cout << "Queue is Empty! Cannot Dequeue." << endl;
  41.             return;
  42.         }
  43.         string removed = arr[head];
  44.         cout << "\nOrder: " << removed << " is ready." << endl;
  45.         head = (head + 1) % size;
  46.         count--;
  47.     }
  48.  
  49.     void display() {
  50.         cout << "\nThe orders are: " << endl;
  51.         for (int i = 0; i < count; i++){
  52.             cout << arr[(head + i) % size] << endl;
  53.         }
  54.     }
  55.  
  56.  
  57.     ~DS() {
  58.         delete[] arr;
  59.     }
  60. };
  61.  
  62. int main() {
  63.  
  64.     int sz;
  65.     cout << "Enter the maximum number of order that can be processed: "; cin >> sz;
  66.  
  67.     DS queue(sz);
  68.  
  69.     int choice;
  70.     string order;
  71.  
  72.     while (true) {
  73.         cout << "\n ----- MENU ----- " << endl;
  74.             cout << "1. Place Order";
  75.         cout << "\n2. Serve Order";
  76.         cout << "\n3. Display Orders";
  77.         cout << "\n4. Exit";
  78.         cout << "\nEnter your choice: ";
  79.             cin >> choice;
  80.  
  81.         if (choice == 1) {
  82.             cout << "\nEnter order: ";
  83.             cin >> order;
  84.             queue.enQueue(order);
  85.         }
  86.        
  87.         else if (choice == 2) {
  88.             queue.deQueue();
  89.         }
  90.  
  91.         else if (choice == 3) {
  92.             queue.display();
  93.         }
  94.  
  95.         else if (choice == 4) {
  96.             cout << "\nExiting..." << endl;
  97.             return 0;
  98.         }
  99.  
  100.         else {
  101.             cout << "Please Enter a valid choice" << endl;
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment