fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Queue
  6. {
  7. public:
  8. int frontItr = -1;
  9. int backItr = -1;
  10.  
  11. int arr[10000];
  12.  
  13. void push(int input)
  14. {
  15. if (frontItr == -1)
  16. {
  17. frontItr = 0;
  18. }
  19.  
  20. arr[++backItr] = input;
  21. }
  22.  
  23. int pop()
  24. {
  25. if(empty() == 1)
  26. {
  27. return -1;
  28. }
  29.  
  30. return arr[frontItr++];
  31. }
  32.  
  33. int size()
  34. {
  35. return backItr - frontItr + 1;
  36. }
  37.  
  38. int empty()
  39. {
  40. if (size() == 0)
  41. {
  42. return 1;
  43. }
  44. else
  45. {
  46. return 0;
  47. }
  48. }
  49.  
  50. int front()
  51. {
  52. if (empty() == 1)
  53. {
  54. return -1;
  55. }
  56.  
  57. return arr[frontItr];
  58. }
  59.  
  60. int back()
  61. {
  62. if (empty() == 1)
  63. {
  64. return -1;
  65. }
  66.  
  67. return arr[backItr];
  68. }
  69. };
  70.  
  71.  
  72. int main()
  73. {
  74. cin.tie(0);
  75. ios::sync_with_stdio(0);
  76.  
  77. int commandCount = 0;
  78.  
  79. cin >> commandCount;
  80. cin.ignore();
  81.  
  82. Queue queue;
  83.  
  84. while (commandCount--)
  85. {
  86. string command;
  87. cin >> command;
  88. cin.ignore();
  89.  
  90. switch (command[0])
  91. {
  92. case 'p':
  93. if (command[1] == 'u')
  94. {
  95. int input;
  96. cin >> input;
  97. cin.ignore();
  98.  
  99. queue.push(input);
  100. }
  101. else if (command[1] == 'o')
  102. {
  103. cout << queue.pop() << endl;
  104. }
  105. break;
  106. case 's':
  107. cout << queue.size() << endl;
  108. break;
  109. case 'e':
  110. cout << queue.empty() << endl;
  111. break;
  112. case 'f':
  113. cout << queue.front() << endl;
  114. break;
  115. case 'b':
  116. cout << queue.back() << endl;
  117. break;
  118. }
  119. }
  120.  
  121. return 0;
  122. }
Success #stdin #stdout 0s 5304KB
stdin
2
empty
size
stdout
0
1