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