fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class DequeException extends Exception {}
  5.  
  6. class Node <T>
  7. {
  8. T val;
  9. Node prev, next;
  10.  
  11. Node(T v, Node p, Node n) {val = v; prev = p; next = n;}
  12. }
  13.  
  14. class MyDeque <T>
  15. {
  16. int size;
  17. Node<T> first, last;
  18.  
  19. MyDeque() {
  20. size = 0;
  21. first = null;
  22. last = null;
  23. }
  24.  
  25. String pushBack(T val) {
  26. if (size > 0) {
  27. Node currentNode = new Node<T> (val, last, null);
  28. last.next = currentNode;
  29. last = currentNode;
  30. size++;
  31. }
  32. else {
  33. Node currentNode = new Node<T> (val, null, null);
  34. last = currentNode;
  35. first = currentNode;
  36. size++;
  37. }
  38. return "ok";
  39. }
  40.  
  41. String pushFront(T val) {
  42. if (size > 0) {
  43. Node currentNode = new Node<T> (val, null, first);
  44. first.prev = currentNode;
  45. first = currentNode;
  46. size++;
  47. }
  48. else {
  49. Node currentNode = new Node<T> (val, null, null);
  50. last = currentNode;
  51. first = currentNode;
  52. size++;
  53. }
  54. return "ok";
  55. }
  56.  
  57. T popFront() throws DequeException {
  58. if(size == 0) throw new DequeException();
  59. T v = first.val;
  60. first = first.next;
  61. size--;
  62. return v;
  63. }
  64.  
  65. T popBack() throws DequeException {
  66. if(size == 0) throw new DequeException();
  67. T v = last.val;
  68. last = last.prev;
  69. size--;
  70. return v;
  71. }
  72.  
  73. T front() throws DequeException {
  74. if(size == 0) throw new DequeException();
  75. return first.val;
  76. }
  77.  
  78. T back() throws DequeException {
  79. if(size == 0) throw new DequeException();
  80. return last.val;
  81. }
  82.  
  83. int getSize() {
  84. return size;
  85. }
  86.  
  87. String clear() {
  88. size = 0;
  89. return "ok";
  90. }
  91.  
  92. String ex() {
  93. return "bye";
  94. }
  95. }
  96.  
  97. class Main
  98. {
  99. public static void main (String[] args) {
  100. Scanner in = new Scanner(System.in);
  101. PrintWriter out = new PrintWriter(System.out);
  102. MyDeque<String> deque = new MyDeque<String>();
  103. String s, val;
  104.  
  105. while(in.hasNext()) {
  106. s = in.next();
  107. if (s.equals("exit")) out.println(deque.ex());
  108. else if (s.equals("clear")) out.println(deque.clear());
  109. else if (s.equals("size")) out.println(deque.getSize());
  110. else if (s.equals("back")) {
  111. try {
  112. out.println(deque.back());
  113. } catch(DequeException e) {
  114. out.println("error");
  115. }
  116. }
  117. else if(s.equals("front")) {
  118. try {
  119. out.println(deque.front());
  120. } catch(DequeException e) {
  121. out.println("error");
  122. }
  123. }
  124. else if (s.equals("pop_back")) {
  125. try {
  126. out.println(deque.popBack());
  127. } catch(DequeException e) {
  128. out.println("error");
  129. }
  130. }
  131. else if (s.equals("pop_front")) {
  132. try {
  133. out.println(deque.popFront());
  134. } catch(DequeException e) {
  135. out.println("error");
  136. }
  137. }
  138. else if(s.equals("push_back")) {
  139. val = in.next();
  140. out.println(deque.pushBack(val));
  141. }
  142. else if(s.equals("push_front")) {
  143. val = in.next();
  144. out.println(deque.pushFront(val));
  145. }
  146. out.flush();
  147. }
  148. }
  149. }
Success #stdin #stdout 0.13s 29496KB
stdin
push_back 1
push_front 2
back
front
size
clear
size
pop_back
exit
stdout
ok
ok
1
2
2
ok
0
error
bye