Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class queue {
  4. public:
  5.     int* a, r, size;
  6.  
  7.     queue() {
  8.         r = 0;
  9.         cout << "\nEnter size of the Queue\n";
  10.         cin >> size;
  11.         cout << "\n\n";
  12.  
  13.         a = new int[size];
  14.     }
  15.  
  16.     int isempty();
  17.     int isfull();
  18.     int push();
  19.     int pop();
  20.     int display();
  21. };
  22.  
  23. /**
  24.  * Check if the array is empty.
  25.  */
  26. int queue::isempty() {
  27.     if (r == -1)
  28.         return 1;
  29.  
  30.     return 0;
  31. }
  32.  
  33. /**
  34.  * Check if the array is full.
  35.  */
  36. int queue::isfull() {
  37.     if (r == (size))
  38.         return 1;
  39.  
  40.     return 0;
  41. }
  42.  
  43. /**
  44.  * Insert a value to the array.
  45.  */
  46. int queue::push() {
  47.     if (isfull()) {
  48.         cout << "\nQueue Overflow\n\n";
  49.         return 0;
  50.  
  51.     }
  52.     else {
  53.         cout << "\nEnter an element: ";
  54.         int val;
  55.         cin >> val;
  56.         a[r] = val;
  57.         cout << val << " has been successfully added \n\n";
  58.         r++;
  59.         display();
  60.     }
  61. }
  62.  
  63. /**
  64.  * Remove the last element of the array.
  65.  */
  66. int queue::pop() {
  67.     if (isempty()) {
  68.         cout << "\n\nQueue Overflow\n";
  69.         return 0;
  70.     }
  71.  
  72.     cout << a[r-1] << " has been removed.";
  73.     for (int i = 0; i <= r; i++) {
  74.         a[i] = a[i];
  75.     }
  76.     r--;
  77.     display();
  78.     cout << "Element deleted sucessfully\n";
  79. }
  80.  
  81. /**
  82.  * Print all the items inside the array.
  83.  */
  84. int queue::display() {
  85.     if (isempty()) {
  86.         cout << "\n\n\nQueue is empty -- No Element to display\n";
  87.         return 0;
  88.     }
  89.     else {
  90.         cout << "\n==================================\n";
  91.         cout << "Items in Queue:\n";
  92.         cout << "==================================\n";
  93.         for (int i = 0; i < r; i++) {
  94.             cout << "[" << i << "] | " << a[i] << "\n";
  95.             cout << "----------------------------------\n";
  96.         }
  97.         cout << "\n";
  98.     }
  99. }
  100.  
  101. /**
  102.  * Main function.
  103.  */
  104. int main() {
  105.     int i = 0, s;
  106.     queue q;
  107.     while (i != 1) {
  108.         cout << "\n********* MENU *******\n";
  109.         cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\n";
  110.         cout << "\n********* MENU *******\n";
  111.         cout << "Enter your option: ";
  112.         cin >> s;
  113.         switch (s) {
  114.             case 1:
  115.                 q.push();
  116.                 break;
  117.             case 2:
  118.                 q.pop();
  119.                 break;
  120.             case 3:
  121.                 q.display();
  122.                 break;
  123.             case 4:
  124.                 i = 1;
  125.                 break;
  126.             default:
  127.                 cout << "\nEnter correct option\n|";
  128.                 break;
  129.         }
  130.     }
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement