Advertisement
hurmawe

Очередь с защитой от ошибок

Dec 18th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace  std;
  4.  
  5. struct queue
  6. {
  7.     uint32_t   cursor_start = 0;
  8.     uint32_t   cursor_end   = 0;
  9.     int64_t    array[100000];
  10.  
  11.     //////////////////
  12.     void push(const int64_t& n)
  13.     {
  14.         array[cursor_end++] = n;
  15.     }
  16.     //////////////////
  17.     int64_t pop()
  18.     {
  19.         return array[cursor_start++];
  20.     }
  21.     /////////////////
  22.     int64_t front()
  23.     {
  24.         return array[cursor_start];
  25.     }
  26.     ///////////////////
  27.     int64_t size()
  28.     {
  29.         return cursor_end-cursor_start;
  30.     }
  31.     ////////////////
  32.     void clear()
  33.     {
  34.         cursor_start = 0;
  35.         cursor_end   = 0;
  36.     }
  37. };
  38. int main() {
  39.     queue primer;
  40.     string stroka;
  41.     while (cin >> stroka)
  42.         if (stroka == "push")
  43.         {
  44.             int64_t vr;
  45.             cin >> vr;
  46.             primer.push(vr);
  47.             cout << "ok" << endl;
  48.         }
  49.         else if(stroka == "pop")
  50.         {
  51.             if(primer.size() == 0)
  52.             {
  53.                 cout << "error" << endl;
  54.             }
  55.             else
  56.             {
  57.                 cout << primer.pop() << endl;
  58.             }
  59.         }
  60.         else if(stroka == "front")
  61.         {
  62.             if(primer.size() == 0)
  63.             {
  64.                 cout << "error" << endl;
  65.             }
  66.             else
  67.             {
  68.                 cout << primer.front() << endl;
  69.             }
  70.         }
  71.         else if(stroka == "size")
  72.         {
  73.             cout << primer.size() << endl;
  74.         }
  75.         else if(stroka == "clear")
  76.         {
  77.             primer.clear();
  78.             cout << "ok" << endl;
  79.         }
  80.         else if(stroka == "exit")
  81.         {
  82.             cout << "bye";
  83.             break;
  84.         }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement