fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class ArrayQueue {
  7. public:
  8. int n, f, r;
  9. int* arr;
  10. int sizeQ;
  11. ArrayQueue(int size) {
  12. this->n = 0;
  13. this->f = 0;
  14. this->r = 0;
  15. this->arr = new int[size];
  16. for (int i = 0; i < size; i++) {
  17. arr[i] = 0;
  18. }
  19. sizeQ = size;
  20. }
  21.  
  22. void push(int data) {
  23. if (empty()) {
  24. arr[0] = data;
  25. }
  26. else {
  27. arr[n] = data;
  28. r = (r + 1) % sizeQ;
  29. }
  30. n++;
  31. }
  32.  
  33. int pop() {
  34. if (empty()) {
  35. return -1;
  36. }
  37. else {
  38. int value = arr[f];
  39. arr[f] = 0;
  40. f = (f + 1) % sizeQ;
  41. n--;
  42. if (empty()) {
  43. f = r = 0;
  44. }
  45. return value;
  46. }
  47. }
  48.  
  49. int size() {
  50. return n;
  51. }
  52.  
  53. int front() {
  54. if (empty()) {
  55. return -1;
  56. }
  57. else {
  58. return arr[f];
  59. }
  60. }
  61.  
  62. int back() {
  63. if (empty()) {
  64. return -1;
  65. }
  66. else {
  67. return arr[r];
  68. }
  69. }
  70.  
  71. bool empty() {
  72. if (n == 0) {
  73. return true;
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79. };
  80.  
  81. int main() {
  82. ArrayQueue Q(10000);
  83. int howMany, data;
  84. string what;
  85. cin >> howMany;
  86. for (int i = 0; i < howMany; i++) {
  87. cin >> what;
  88. if (what == "push") {
  89. cin >> data;
  90. Q.push(data);
  91. }
  92. else if (what == "pop") {
  93. cout << Q.pop() << endl;
  94. }
  95. else if (what == "front") {
  96. cout << Q.front() << endl;
  97. }
  98. else if (what == "back") {
  99. cout << Q.back() << endl;
  100. }
  101. else if (what == "size") {
  102. cout << Q.size() << endl;
  103. }
  104. else if (what == "empty") {
  105. cout << Q.empty() << endl;
  106. }
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0s 4376KB
stdin
5
push 1
push 2
pop
push 3
pop
stdout
1
3