Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. /**
  2. * The Class CustomLinkedList.
  3. * It's a linkedlist. It adds, adds to the end, shows the next and checks if there's a next.
  4. * Hell, checks if it's empty, and even gives size.
  5. * @param <T> the generic type
  6. * @author Nikolas McGovern
  7. * @author Samuel Schoeneberger
  8. */
  9. public class CustomLinkedList<T> {
  10.  
  11. /** The head node. */
  12. private Node head;
  13.  
  14. /** The iterator node. */
  15. private Node iterator;
  16.  
  17. /**
  18. * Instantiates a new CustomLinkedList.
  19. * sets head to a node with null properties
  20. */
  21. public CustomLinkedList() {
  22. head = new Node(null, null);
  23. resetIterator();
  24. }
  25.  
  26. /**
  27. * Reset iterator.
  28. */
  29. public void resetIterator() {
  30. iterator = head;
  31. }
  32.  
  33. /**
  34. * Checks if there's a next node.
  35. * @return true, if successful
  36. */
  37. public boolean hasNext() {
  38. return iterator.hasNext();
  39. }
  40.  
  41. /**
  42. * returns the next Node.
  43. * @return the next node.
  44. */
  45. public T next() {
  46. if (iterator.hasNext()) {
  47. return iterator.next();
  48. } else {
  49. return null;
  50. }
  51. }
  52.  
  53. public void add(T t) {
  54. if (contains(t)) {
  55. remove(t);
  56. }
  57. Node newNode = new Node(t, head);
  58. head = newNode;
  59. }
  60.  
  61. public int indexOf(T t) {
  62. for (int i = 0; i < size(); i++) {
  63. if (lookAtItemN(i).equals(t)) {
  64. return i;
  65. }
  66. }
  67. return -1;
  68. }
  69.  
  70. public boolean contains(T t) {
  71. return indexOf(t) != -1;
  72. }
  73.  
  74. /**
  75. * Look at item at position index.
  76. * @param index the index to look at.
  77. * @return the node at position index
  78. */
  79. public T lookAtItemN(int index) {
  80. if (index < 0) {
  81. return null;
  82. }
  83. iterator.resetIterator();
  84. for (int i = 0; i < index; i++) {
  85. iterator.next();
  86. }
  87. return iterator.data;
  88. }
  89.  
  90. /**
  91. * Search for node with this data.
  92. * @param data to look for
  93. * @return the node with that data.
  94. */
  95. public T searchForNodeData(T t) {
  96. for (int i = 0; i < size(); i++) {
  97. if (lookAtItemN(i).equals(t)) {
  98. return lookAtItemN(i);
  99. }
  100. }
  101. return null;
  102. }
  103.  
  104. public T remove(T t) {
  105. resetIterator();
  106. while(iterator.hasNext()) {
  107. if (iterator.next.data == null) {
  108. return t;
  109. }
  110. if (iterator.next.data.equals(t)) {
  111. iterator.next = iterator.next.next;
  112. }
  113. iterator = iterator.next;
  114. }
  115. return t;
  116. }
  117.  
  118. /**
  119. * Returns size of list.
  120. * @return the size of list.
  121. */
  122. public int size() {
  123. if (head == null) return 0;
  124. int size = 0;
  125. resetIterator();
  126. while (iterator.hasNext()) {
  127. size++;
  128. iterator.next();
  129. }
  130. resetIterator();
  131. return size;
  132. }
  133.  
  134. public String output() {
  135. String s = "";
  136. for (int i = 0; i < size(); i++) {
  137. s += lookAtItemN(i) + " ";
  138. }
  139. return s;
  140. }
  141.  
  142. public void moveToFront(T t) {
  143. resetIterator();
  144. // System.out.println("MTF 0: " + t);
  145. // System.out.println("MTF 1: " + output());
  146. this.remove(t);
  147. // System.out.println("MTF 2: " + output());
  148. this.add(t);
  149. // System.out.println("MTF 3: " + output());
  150. }
  151.  
  152. /**
  153. * The Private Class Node.
  154. * Handles next, hasNext, resetIterator on its own.
  155. */
  156. private class Node {
  157.  
  158. /** The data. */
  159. public T data;
  160.  
  161. /** The next node. */
  162. public Node next;
  163.  
  164. /**
  165. * Instantiates a new node.
  166. * @param t the data
  167. * @param n the next node
  168. */
  169. public Node(T t, Node n) {
  170. data = t;
  171. next = n;
  172. }
  173.  
  174. /**
  175. * Reset iterator.
  176. */
  177. public void resetIterator() {
  178. iterator = head;
  179. }
  180.  
  181. /**
  182. * Returns the next data.
  183. * @return the next node's data.
  184. */
  185. public T next() {
  186. if (hasNext()) {
  187. iterator = next;
  188. return iterator.data;
  189. } else {
  190. return null;
  191. }
  192. }
  193.  
  194. /**
  195. * Checks if node has a next node.
  196. * @return true, if successful
  197. */
  198. public boolean hasNext() {
  199. return iterator.next != null;
  200. }
  201.  
  202. }
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement