Advertisement
Guest User

Queue

a guest
Dec 13th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int SIZE = 101;
  4. int que[SIZE] = {};
  5. int head, tail;
  6.  
  7. void push(int x)
  8. {
  9.   que[head] = x;
  10.   head = (head + 1) % SIZE;
  11. }
  12.  
  13. void pop()
  14. {
  15.   tail = (tail + 1) % SIZE;
  16. }
  17.  
  18. int front()
  19. {
  20.   return que[tail];
  21. }
  22.  
  23. int size()
  24. {
  25.   return (head + SIZE - tail) % SIZE;
  26. }
  27.  
  28. void clear()
  29. {
  30.   head = tail = 0;
  31. }
  32.  
  33. int main()
  34. {
  35.   string command;
  36.   while(1)
  37.   {
  38.     cin >> command;
  39.     if (command == "exit") break;
  40.     if (command == "pop")
  41.     {
  42.       cout << front();
  43.       pop();
  44.     }
  45.     else if (command == "front")
  46.     {
  47.       cout << front();
  48.     }
  49.     else if (command == "size")
  50.     {
  51.       cout << size();
  52.     }
  53.     else if (command == "clear")
  54.     {
  55.       clear();
  56.       cout << "ok";
  57.     }
  58.     else if (command == "push")
  59.     {
  60.       int x;
  61.       cin >> x;
  62.       push(x);
  63.       cout << "ok";
  64.     }
  65.     cout << endl;
  66.   }
  67.   cout << "bye";
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement