fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5.  
  6. class Deque
  7. {
  8. static final int maxSize = 10000;
  9. int size;
  10. int head, end;
  11. int[] data;
  12.  
  13. public Deque()
  14. {
  15. data = new int[maxSize];
  16. head = 0;
  17. end = 0;
  18. size = 0;
  19. }
  20.  
  21. public void push_front(int e)
  22. {
  23. if (size == maxSize)
  24. {
  25. System.out.println("Deque is full.");
  26. return;
  27. }
  28. else if (size==0)
  29. {
  30. end = head;
  31. data[head] = e;
  32. size++;
  33. }
  34. else
  35. {
  36. head++;
  37. if (head>=maxSize)
  38. head = 0;
  39. data[head] = e;
  40. size++;
  41. }
  42. System.out.println("ok");
  43. }
  44.  
  45. public void push_back(int e)
  46. {
  47. if (size == maxSize)
  48. {
  49. System.out.println("Deque is full.");
  50. return;
  51. }
  52. else if (size==0)
  53. {
  54. head = end;
  55. data[end] = e;
  56. size++;
  57. }
  58. else
  59. {
  60. end--;
  61. if (end<0)
  62. end = maxSize-1;
  63. data[end] = e;
  64. size++;
  65. }
  66. System.out.println("ok");
  67. }
  68.  
  69. public void clear()
  70. {
  71. head = 0;
  72. end = 0;
  73. size = 0;
  74. System.out.println("ok");
  75. }
  76.  
  77. public int size()
  78. {
  79. return size;
  80. }
  81.  
  82. public int back()
  83. {
  84. if (size!=0)
  85. return data[end];
  86. else
  87. return -1;
  88. }
  89.  
  90. public int front()
  91. {
  92. if (size!=0)
  93. return data[head];
  94. else
  95. return -1;
  96. }
  97.  
  98. public int pop_back()
  99. {
  100. if (size!=0)
  101. {
  102. int tmp = data[end];
  103. end++;
  104. if (end>=maxSize)
  105. end = 0;
  106. size --;
  107. return tmp;
  108. }
  109. else
  110. return -1;
  111. }
  112.  
  113. public int pop_front()
  114. {
  115. if (size!=0)
  116. {
  117. int tmp = data[head];
  118. head--;
  119. if (head<0)
  120. head = maxSize - 1;
  121. size --;
  122. return tmp;
  123. }
  124. else
  125. return -1;
  126. }
  127. }
  128.  
  129. class task
  130. {
  131. static public void main (String[] args) throws IOException
  132. {
  133. Deque dq = new Deque();
  134. String str;
  135.  
  136. while((str = br.readLine()) != null)
  137. {
  138. String[] inp = str.split(" ");
  139. if (inp[0].equals("push_front"))
  140. {
  141. dq.push_front(Integer.parseInt(inp[1]));
  142. }
  143. else if (inp[0].equals("push_back"))
  144. {
  145. dq.push_back(Integer.parseInt(inp[1]));
  146. }
  147. else if (inp[0].equals("pop_back"))
  148. {
  149. System.out.println(dq.pop_back());
  150. }
  151. else if (inp[0].equals("pop_front"))
  152. {
  153. System.out.println(dq.pop_front());
  154. }
  155. else if (inp[0].equals("front"))
  156. {
  157. System.out.println(dq.front());
  158. }
  159. else if (inp[0].equals("back"))
  160. {
  161. System.out.println(dq.back());
  162. }
  163. else if (inp[0].equals("size"))
  164. {
  165. System.out.println(dq.size());
  166. }
  167. else if (inp[0].equals("clear"))
  168. {
  169. dq.clear();
  170. }
  171. else if (inp[0].equals("exit"))
  172. {
  173. System.out.println("bye");
  174. br.close();
  175. return;
  176. }
  177. }
  178. }
  179. }
Success #stdin #stdout 0.04s 4386816KB
stdin
push_back 3
push_front 14
size
clear
push_front 1
back
push_back 2
front
pop_back
size
pop_front
size
exit
stdout
ok
ok
2
ok
ok
1
ok
1
2
1
1
0
bye