Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int MAX = (10000 + 1) * 2;
  6.  
  7. int head = MAX / 2, tail = MAX / 2;
  8. int arr[MAX];
  9.  
  10. // stl 사용하지 않고 구현
  11. void push_front(int num)
  12. {
  13. arr[--head] = num;
  14. }
  15.  
  16. void push_back(int num)
  17. {
  18. arr[tail++] = num;
  19. }
  20.  
  21. void pop_front(void)
  22. {
  23. head++;
  24. }
  25.  
  26. void pop_back(void)
  27. {
  28. tail--;
  29. }
  30.  
  31. int front(void)
  32. {
  33. return arr[head];
  34. }
  35.  
  36. int back(void)
  37. {
  38. return arr[tail - 1];
  39. }
  40.  
  41. int main(void)
  42. {
  43. ios_base::sync_with_stdio(0);
  44. cin.tie(0);
  45. int N;
  46. cin >> N;
  47.  
  48. for (int n = 0; n < N; n++)
  49. {
  50. string s;
  51. cin >> s;
  52.  
  53. if (s == "push_front")
  54. {
  55. int num;
  56. cin >> num;
  57.  
  58. push_front(num);
  59. }
  60. else if (s == "push_back")
  61. {
  62. int num;
  63. cin >> num;
  64.  
  65. push_back(num);
  66. }
  67. else if (s == "pop_front")
  68. {
  69. if (head == tail)
  70. cout << -1 << "\n";
  71. else
  72. {
  73. cout << front() << "\n";
  74. pop_front();
  75. }
  76. }
  77. else if (s == "pop_back")
  78. {
  79. if (head == tail)
  80. cout << -1 << "\n";
  81. else
  82. {
  83. cout << back() << "\n";
  84. pop_back();
  85. }
  86. }
  87. else if (s == "size")
  88. {
  89. cout << tail - head << "\n";
  90. }
  91. else if (s == "empty")
  92. {
  93. bool flag = (head == tail);
  94. cout << flag << "\n";
  95. }
  96. else if (s == "front")
  97. {
  98. if (head == tail)
  99. cout << -1 << "\n";
  100. else
  101. cout << front() << "\n";
  102. }
  103. else
  104. {
  105. if (head == tail)
  106. cout << -1 << "\n";
  107. else
  108. cout << back() << "\n";
  109. }
  110. }
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement