fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #define SIZE 10000
  6. struct deque
  7. {
  8. int storage[SIZE];
  9. int _size = 0, start = 0, end = 0;
  10. void push_front(int n)
  11. {
  12. if (_size == SIZE) { cout << "Full\n"; }
  13. else if (_size == 0) { storage[start] = n; _size++; }
  14. else {
  15. if (start == 0) { start = SIZE - 1; storage[start] = n; _size++; }
  16. else { start--; storage[start] = n; _size++; }
  17. }
  18. }
  19. void push_back(int n)
  20. {
  21. if (_size == SIZE) { cout << "Full\n"; }
  22. else if (_size == 0) { storage[end] = n; _size++; }
  23. else {
  24. if (end == SIZE-1) { end = 0; storage[end] = n; _size++; }
  25. else { end++; storage[end] = n; _size++; }
  26. }
  27. }
  28. int pop_front()
  29. {
  30. int b;
  31. if (_size != 0 && (start == end)) { b = storage[start]; _size--; return b; }
  32. else if (_size == 0) { return -1; }
  33. else
  34. {
  35. b = storage[start];
  36. if (start == SIZE - 1) { start = 0; _size--; }
  37. else { start++; _size--; }
  38. return b;
  39. }
  40. }
  41. int pop_back()
  42. {
  43. int b;
  44. if (_size != 0 && (start == end)) { b = storage[end]; _size--; return b; }
  45. else if (_size == 0) { return -1; }
  46. else
  47. {
  48. b = storage[end];
  49. if (end == 0) { end = SIZE - 1; _size--; }
  50. else { end--; _size--; }
  51. return b;
  52. }
  53. }
  54. int front() { if (_size == 0) return -1; else return storage[start]; }
  55. int back() { if (_size == 0) return -1; else return storage[end]; }
  56. int size() { return _size; }
  57. void clear()
  58. {
  59. _size = 0;
  60. start = end = 0;
  61. }
  62. };
  63.  
  64. int main()
  65. {
  66. deque d;
  67. string s;
  68. int n;
  69. while (cin >> s)
  70. {
  71. if (s == "push_front")
  72. {
  73. cin >> n;
  74. d.push_front(n);
  75. cout << "ok\n";
  76. }
  77. else if (s == "push_back")
  78. {
  79. cin >> n;
  80. d.push_back(n);
  81. cout << "ok\n";
  82. }
  83. else if (s == "pop_front")
  84. {
  85. cout << d.pop_front() << endl;
  86. }
  87. else if (s == "pop_back")
  88. {
  89. cout << d.pop_back() << endl;
  90. }
  91. else if (s == "front")
  92. {
  93. cout << d.front() << endl;
  94. }
  95. else if (s == "back")
  96. {
  97. cout << d.back() << endl;
  98. }
  99. else if (s == "size")
  100. {
  101. cout << d.size() << endl;
  102. }
  103. else if (s == "clear")
  104. {
  105. d.clear();
  106. cout << "ok\n";
  107. }
  108.  
  109. else if (s == "exit") {
  110. cout << "bye\n";
  111. return 0;
  112. }
  113. }
  114. return 0;
  115. }
Success #stdin #stdout 0s 3468KB
stdin
push_front 1
push_back 12
back
front 
size
pop_front
size
exit

stdout
ok
ok
12
1
2
1
1
bye