fork download
  1. import java.lang.*;
  2.  
  3. class Ideone
  4. {
  5. static interface Iterator<T> {
  6. boolean hasNext();
  7. T next();
  8. }
  9.  
  10. static interface List<T> {
  11. void add(T data);
  12. void insertFirst(T data);
  13. T removeFirst();
  14. Iterator<T> iterator();
  15. boolean isEmpty();
  16. int getSize();
  17. }
  18.  
  19. static interface Queue<T> {
  20. T dequeue();
  21. void enqueue(T data);
  22. boolean isEmpty();
  23. int getSize();
  24. }
  25.  
  26. static interface Stack<T> {
  27. T pop();
  28. T peek();
  29. void push(T data);
  30. boolean isEmpty();
  31. int getSize();
  32. }
  33.  
  34. static class MyLinkedList<T> implements List<T>, Queue<T>, Stack<T> {
  35. class ListStructure {
  36. T data;
  37. ListStructure next = null;
  38. ListStructure(T data) { this.data = data; }
  39. }
  40.  
  41. ListStructure head = null;
  42. ListStructure tail = null;
  43. int size = 0;
  44.  
  45. MyLinkedList() {}
  46.  
  47. public int getSize() { return size; }
  48. public boolean isEmpty() { return size == 0; }
  49.  
  50. public void add(T data) {
  51. ListStructure node = new ListStructure(data);
  52. if (tail == null) {
  53. head = tail = node;
  54. } else {
  55. tail.next = node;
  56. tail = node;
  57. }
  58. ++size;
  59. }
  60.  
  61. public void insertFirst(T data) {
  62. ListStructure node = new ListStructure(data);
  63. if (tail == null) {
  64. head = tail = node;
  65. } else {
  66. node.next = head;
  67. head = node;
  68. }
  69. ++size;
  70. }
  71.  
  72. public T removeFirst() {
  73. if (head == null) return null;
  74. ListStructure node = head;
  75. head = node.next;
  76. --size;
  77. return node.data;
  78. }
  79.  
  80. public T dequeue() {
  81. return removeFirst();
  82. }
  83.  
  84. public void enqueue(T data) {
  85. add(data);
  86. }
  87.  
  88. public T pop() {
  89. return removeFirst();
  90. }
  91.  
  92. public T peek() {
  93. if (head == null) return null;
  94. return head.data;
  95. }
  96.  
  97. public void push(T data) {
  98. insertFirst(data);
  99. }
  100.  
  101. public Iterator<T> iterator() {
  102. return new Iterator<T>() {
  103. private ListStructure node = head;
  104. public boolean hasNext() {
  105. return node != null;
  106. }
  107. public T next() {
  108. if (node == null) return null;
  109. ListStructure tmp = node;
  110. node = tmp.next;
  111. return tmp.data;
  112. }
  113. };
  114. }
  115. }
  116.  
  117. public static void main (String[] args) throws java.lang.Exception
  118. {
  119. List<Integer> list1 = new MyLinkedList<Integer>();
  120. List<String> list2 = new MyLinkedList<String>();
  121. Queue<Integer> queue = new MyLinkedList<Integer>();
  122. Stack<String> stack = new MyLinkedList<String>();
  123.  
  124. for (int i = 1; i <= 5; ++i) {
  125. list1.add(Integer.valueOf(i * i + 1));
  126. list2.add("hoge[" + i + "] = " + (i * i));
  127. stack.push("stk = " + i);
  128. queue.enqueue(Integer.valueOf(1 << i));
  129. }
  130.  
  131. for (Iterator<Integer> itr = list1.iterator(); itr.hasNext(); ) {
  132. System.out.println(itr.next());
  133. }
  134.  
  135. for (Iterator<String> itr = list2.iterator(); itr.hasNext(); ) {
  136. System.out.println(itr.next());
  137. }
  138.  
  139. while (!queue.isEmpty()) {
  140. System.out.println(queue.dequeue());
  141. }
  142.  
  143. while (!stack.isEmpty()) {
  144. System.out.println(stack.pop());
  145. }
  146. }
  147. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
2
5
10
17
26
hoge[1] = 1
hoge[2] = 4
hoge[3] = 9
hoge[4] = 16
hoge[5] = 25
2
4
8
16
32
stk = 5
stk = 4
stk = 3
stk = 2
stk = 1