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