fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. struct Deque
  6. {
  7. vector<int> box1;
  8. vector<int> box2;
  9. int n, box_size=0;
  10. void push_front(int n)
  11. {
  12. box1.push_back(n);
  13. box_size++;
  14. }
  15. void push_back(int n)
  16. {
  17. box2.push_back(n);
  18. box_size++;
  19. }
  20. int pop_front()
  21. {
  22. if(box1.size()>0)
  23. {
  24. int a=box1[box1.size()-1];
  25. box1.erase(box1.end()-1);
  26. box_size--;
  27. return a;
  28. }
  29. else if(box2.size()>0)
  30. {
  31. int a=box2[0];
  32. box2.erase(box2.begin());
  33. box_size--;
  34. return a;
  35. }
  36. return 1;
  37. }
  38. int pop_back()
  39. {
  40. if(box2.size()>0)
  41. {
  42. int a=box2[box2.size()-1];
  43. box2.erase(box2.end()-1);
  44. box_size--;
  45. return a;
  46. }
  47. else if(box1.size()>0)
  48. {
  49. int a=box1[0];
  50. box1.erase(box1.begin());
  51. box_size--;
  52. return a;
  53. }
  54. return 1;
  55. }
  56. int front()
  57. {
  58. if(box1.size()>0)
  59. {
  60. int a=box1[box1.size()-1];
  61. return a;
  62. }
  63. else if(box2.size()>0)
  64. {
  65. int a=box2[0];
  66. return a;
  67. }
  68. return 1;
  69. }
  70. int back()
  71. {
  72. if(box2.size()>0)
  73. {
  74. int a=box2[box2.size()-1];
  75. return a;
  76. }
  77. else if(box1.size()>0)
  78. {
  79. int a=box1[0];
  80. return a;
  81. }
  82. return 1;
  83. }
  84. int size()
  85. {
  86. return box_size;
  87. }
  88. string clear()
  89. {
  90. box1.erase(box1.begin(), box1.end());
  91. box2.erase(box2.begin(), box2.end());
  92. box_size=0;
  93. return "ok";
  94. }
  95. string exit()
  96. {
  97. return "bye";
  98. }
  99. };
  100. int main()
  101. {
  102. Deque a;
  103. string s;
  104. while(cin>>s)
  105. {
  106. if(s=="push_front")
  107. {
  108. int n;
  109. cin>>n;
  110. a.push_front(n);
  111. cout<<"ok"<<endl;
  112. }
  113. if(s=="push_back")
  114. {
  115. int n;
  116. cin>>n;
  117. a.push_back(n);
  118. cout<<"ok"<<endl;
  119. }
  120. if(s=="pop_front")
  121. {
  122. cout<<a.pop_front()<<endl;
  123. }
  124. if(s=="pop_back")
  125. {
  126. cout<<a.pop_back()<<endl;
  127. }
  128. if(s=="front")
  129. {
  130. cout<<a.front()<<endl;
  131. }
  132. if(s=="back")
  133. {
  134. cout<<a.back()<<endl;
  135. }
  136. if(s=="size")
  137. {
  138. cout<<a.size()<<endl;
  139. }
  140. if(s=="clear"){
  141. a.clear();
  142. cout<<a.clear()<<endl;
  143. }
  144. if(s=="exit")
  145. {
  146. cout<<a.exit()<<endl;
  147. break;
  148. }
  149. }
  150. return 0;
  151. }
Success #stdin #stdout 0s 15232KB
stdin
size
push_back 8
push_front 4
size
front
back
push_back 3
pop_front
front
pop_back
back
exit
stdout
0
ok
ok
2
4
8
ok
4
8
3
8
bye