fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main {
  6. public static class Node {
  7. public int value;
  8. public Node next;
  9.  
  10. public Node() {
  11. value = 0;
  12. next = null;
  13. }
  14. public Node(int newValue, Node newNext) {
  15. value = newValue;
  16. next = newNext;
  17. }
  18. }
  19.  
  20. public static class Queue {
  21. private int size;
  22. private Node first;
  23. private Node last;
  24.  
  25. public Queue() {
  26. size = 0;
  27. first = null;
  28. last = null;
  29. }
  30.  
  31. public void push(int v) {
  32. Node newNode = new Node(v, null);
  33. if (size == 0) {
  34. first = last = newNode;
  35. } else {
  36. last.next = newNode;
  37. last = newNode;
  38. }
  39. size++;
  40. }
  41. public boolean pop() {
  42. boolean ret = false;
  43. if (size > 0) {
  44. int val = first.value;
  45. first = first.next;
  46. size--;
  47. System.out.print(val);
  48. ret = true;
  49. }
  50. return ret;
  51. }
  52. public boolean front() {
  53. boolean ret = false;
  54. if (size > 0) {
  55. System.out.print(first.value);
  56. ret = true;
  57. }
  58. return ret;
  59. }
  60. public int getSize() {
  61. return size;
  62. }
  63. public void clear() {
  64. first = null;
  65. last = null;
  66. size = 0;
  67. }
  68. }
  69. public static void main(String[] args) {
  70. Queue q = new Queue();
  71. String s;
  72. int n;
  73. Scanner scan = new Scanner(System.in);
  74. while ((s = scan.next()) != null) {
  75. switch(s) {
  76. case "size": System.out.println(q.getSize()); break;
  77. case "push":
  78. n = scan.nextInt();
  79. q.push(n);
  80. System.out.println("ok");
  81. break;
  82. case "pop":
  83. if (q.pop() == false) {
  84. System.out.print("error");
  85. }
  86. System.out.println();
  87. break;
  88. case "front":
  89. if (q.front() == false) {
  90. System.out.print("error");
  91. }
  92. System.out.println();
  93. break;
  94. case "clear":
  95. q.clear();
  96. System.out.println("ok");
  97. break;
  98. case "exit":
  99. System.out.println("bye");
  100. return;
  101. default: break;
  102. }
  103. }
  104. }
  105. }
Success #stdin #stdout 0.06s 711680KB
stdin
size
push 1
size
push 2
size
push 3
size
exit
stdout
0
ok
1
ok
2
ok
3
bye