fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static LinkedList<String> list = new LinkedList<String>();
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Ideone.test();
  15. }
  16.  
  17. public static void test() {
  18. list.add("oi");
  19. list.add("ola");
  20. list.add("hello");
  21.  
  22. System.out.println(list);
  23.  
  24. list.remove("hello");
  25.  
  26. System.out.println(list);
  27. }
  28.  
  29. public static class LinkedList<V> {
  30. private int size;
  31. private Node<V> root;
  32. private Node<V> last;
  33.  
  34. public void add(V value) {
  35. System.out.println("add('" + value + "')");
  36. if(root == null) {
  37. size++;
  38. root = last = new Node<V>(value);
  39. return;
  40. }
  41. System.out.println("addNext('" + value + "')");
  42. size++;
  43. Node<V> next = new Node<V>(value);
  44. last.next = next;
  45. last = last.next;
  46. }
  47.  
  48. public void remove(V value) {
  49. System.out.println("remove('" + value + "')");
  50. if(root == null) {
  51. return;
  52. }
  53. if(root.getValue().equals(value)) {
  54. size--;
  55. if(root.next == null) {
  56. root = last = null;
  57. return;
  58. }
  59. root = root.next;
  60. return;
  61. }
  62. removeNext(root, value);
  63. }
  64.  
  65. private void removeNext(Node<V> newRoot, V value) {
  66. System.out.println("removeNext('" + value + "')");
  67. if(newRoot.next == null) {
  68. return;
  69. }
  70. if(newRoot.next.getValue().equals(value)) {
  71. size--;
  72. if(newRoot.next == null) {
  73. newRoot.next = null;
  74. last = newRoot;
  75. return;
  76. }
  77. newRoot.next = newRoot.next.next;
  78. return;
  79. }
  80. removeNext(newRoot.next, value);
  81. }
  82.  
  83. public V get(int index) {
  84. System.out.println("get('" + index + "')");
  85. if(root == null || index > (size - 1)) {
  86. return null;
  87. }
  88.  
  89. if(index == (size - 1)) {
  90. return last.getValue();
  91. }
  92.  
  93. int count = 0;
  94. if(count == index) {
  95. System.out.println("getNextValue('" + index + "=" + root.getValue() + "')");
  96. return root.getValue();
  97. }
  98. return getNext(root.next, count+1, index);
  99. }
  100.  
  101. private V getNext(Node<V> next, int count, int index) {
  102. System.out.println("getNext('" + count + "==" + index + "')");
  103. if(next == null) {
  104. System.out.println("getNextValue('" + index + "=null')");
  105. return null;
  106. }
  107.  
  108. if(count == index) {
  109. System.out.println("getNextValue('" + index + "=" + next.getValue() + "')");
  110. return next.getValue();
  111. }
  112. return getNext(next.next, count+1, index);
  113. }
  114.  
  115. public int getSize() {
  116. return size;
  117. }
  118.  
  119. public String toString() {
  120. StringBuilder sb = new StringBuilder();
  121. sb.append("[");
  122. for(int i = 0; i < size; i++) {
  123. sb.append(i).append("=").append(get(i));
  124. if(i != (size - 1)) {
  125. sb.append(", ");
  126. }
  127. }
  128. sb.append("]");
  129. return sb.toString();
  130. }
  131.  
  132. public String printValues() {
  133. return root.printNodeAndChilds();
  134. }
  135. }
  136.  
  137. public static class Node<V> {
  138. private V value;
  139. public Node<V> next;
  140. public Node<V> previous;
  141.  
  142. public Node(V value) {
  143. this.value = value;
  144. }
  145.  
  146. public V getValue() {
  147. return value;
  148. }
  149.  
  150. public String printNodeAndChilds() {
  151. StringBuilder sb = new StringBuilder();
  152. sb.append("[0=").append(value);
  153. Node<V> nextNode = next;
  154. int count = 1;
  155. while(nextNode != null) {
  156. sb.append(", ").append(count).append("=").append(nextNode.getValue());
  157. nextNode = nextNode.next;
  158. count++;
  159. }
  160. sb.append("]");
  161. return sb.toString();
  162. }
  163.  
  164. public String toString() {
  165. return value.toString();
  166. }
  167.  
  168. }
  169. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
add('oi')
add('ola')
addNext('ola')
add('hello')
addNext('hello')
get('0')
getNextValue('0=oi')
get('1')
getNext('1==1')
getNextValue('1=ola')
get('2')
[0=oi, 1=ola, 2=hello]
remove('hello')
removeNext('hello')
removeNext('hello')
get('0')
getNextValue('0=oi')
get('1')
[0=oi, 1=hello]