Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. package list;
  2.  
  3. import node.NodeI;
  4.  
  5. import java.util.NoSuchElementException;
  6. import java.util.Objects;
  7.  
  8. public class LinkedList<T> {
  9. private T data;
  10. private LinkedList<T> next = null;
  11. private NodeI<T> head;
  12.  
  13. /*
  14. The list can either be empty when initialised,
  15. or the initialisation may contain a head
  16. */
  17.  
  18. //TODO: implement more recursivity, just linked list, w/out node, empty list entity
  19. //TODO: implement more functionality: addAt, removeAt, findIndexOfElement, getElementAtIndex
  20.  
  21. public LinkedList(T data) {
  22. this.data = data;
  23. }
  24.  
  25. public LinkedList<T> getNext() { return this.next; }
  26.  
  27. public void setNext(LinkedList<T> next) {
  28. this.next = next;
  29. }
  30.  
  31. public T getData() {
  32. return data;
  33. }
  34.  
  35. public void setData(T data) {
  36. this.data = data;
  37. }
  38.  
  39. public LinkedList<T> addHead(T data) {
  40. LinkedList<T> tmp = new LinkedList<>(data);
  41. tmp.setNext(this);
  42. return tmp;
  43. }
  44.  
  45. public void addNode(T data) {
  46. addNode(this, data);
  47. }
  48.  
  49. private void addNode(LinkedList<T> currentNode, T data) {
  50. if (currentNode.getNext() == null) {
  51. currentNode.setNext(new LinkedList<>(data));
  52. return;
  53. }
  54.  
  55. addNode(currentNode.getNext(), data);
  56. }
  57.  
  58. public String toString() {
  59. return traverse(this);
  60. }
  61.  
  62. private String traverse(LinkedList<T> currentNode) {
  63.  
  64. StringBuilder stringBuilder = new StringBuilder(currentNode.getData() + " ");
  65.  
  66. if (currentNode.getNext() == null) {
  67. return stringBuilder.toString();
  68. }
  69.  
  70. stringBuilder.append(traverse(currentNode.getNext()));
  71.  
  72. return stringBuilder.toString();
  73. }
  74.  
  75. public int getListSize() {
  76. return getListSize(this);
  77. }
  78.  
  79. private int getListSize(LinkedList<T> currentNode) {
  80. if (currentNode.getNext() == null) {
  81. return 1;
  82. }
  83.  
  84. return 1 + getListSize(currentNode.getNext());
  85. }
  86.  
  87. public LinkedList<T> getNodeAtIndex(int index) {
  88. int size = getListSize(this);
  89. if (index > size)
  90. throw new NoSuchElementException();
  91.  
  92. if (index < 0)
  93. throw new NoSuchElementException();
  94.  
  95. LinkedList<T> node = getNodeAtIndex(0, this, index);
  96. return node;
  97. }
  98.  
  99. private LinkedList<T> getNodeAtIndex(int currentIndex, LinkedList<T> currentNode, int index) {
  100. if (currentIndex == index) {
  101. return currentNode;
  102. }
  103.  
  104. return getNodeAtIndex(currentIndex + 1, currentNode.getNext(), index);
  105. }
  106.  
  107. public int getIndexOfNode(LinkedList<T> node) {
  108. return getIndexOfNode(0, this, node);
  109. }
  110.  
  111. private int getIndexOfNode(int index, LinkedList<T> currentNode, LinkedList<T> node) {
  112. if (currentNode == null)
  113. return -1;
  114.  
  115. if (currentNode.equals(node)) {
  116. return index;
  117. }
  118.  
  119. return getIndexOfNode(index + 1, currentNode.getNext(), node);
  120. }
  121.  
  122. public LinkedList<T> removeNode(T node) {
  123. return removeNode(this, null, node);
  124. }
  125.  
  126. private LinkedList<T> removeNode(LinkedList<T> currentNode, LinkedList<T> previousNode, T node) {
  127. if (currentNode.getData().equals(node)) {
  128.  
  129. //if it's the head of the list
  130. if (null == previousNode) {
  131. LinkedList<T> tmp = currentNode;
  132. currentNode = tmp.getNext();
  133. return currentNode;
  134. }
  135. else {
  136. previousNode.setNext(currentNode.getNext());
  137. return previousNode;
  138. }
  139. }
  140.  
  141. return removeNode(currentNode.getNext(), currentNode, node);
  142. }
  143.  
  144. //TODO: figure out a way to be able to add element as head and as body and not lose the references
  145. public void addAt(int index, T data) {
  146.  
  147. if (!checkIfInBounds(index)) {
  148. throw new IndexOutOfBoundsException();
  149. }
  150.  
  151. if (index == 0) {
  152. this.addHead(data);
  153. return;
  154. }
  155.  
  156. addAt(this, 0, index, data);
  157. }
  158.  
  159. private LinkedList<T> addAt(LinkedList<T> currentNode, int currentIndex, int index, T data) {
  160. if (currentIndex == index-1) {
  161. LinkedList<T> node = new LinkedList<>(data);
  162. node.setNext(currentNode.getNext());
  163. currentNode.setNext(node);
  164. return currentNode;
  165. }
  166.  
  167. return addAt(currentNode.getNext(), currentIndex + 1, index, data);
  168. }
  169.  
  170. //TODO: figure out a way to not lose previous node references when removing
  171. public LinkedList<T> removeAt(int index) {
  172.  
  173. if (!checkIfInBounds(index)) {
  174. throw new IndexOutOfBoundsException();
  175. }
  176.  
  177. return removeAt(this, null, 0, index);
  178.  
  179. }
  180.  
  181. private LinkedList<T> removeAt(LinkedList<T> currentNode, LinkedList<T> previousNode, int currentIndex, int index) {
  182. if (currentIndex == index) {
  183. if (null == previousNode) {
  184. LinkedList<T> tmp = currentNode;
  185. currentNode = tmp.getNext();
  186. return currentNode;
  187. }
  188. else {
  189. previousNode.setNext(currentNode.getNext());
  190. return previousNode;
  191. }
  192. }
  193.  
  194. return removeAt(currentNode.getNext(), currentNode, currentIndex + 1, index);
  195. }
  196.  
  197. private boolean checkIfInBounds(int index) {
  198. return !(index < 0 || index > getListSize());
  199. }
  200.  
  201. //reverse the node order in the list
  202. public void reverse() {
  203. NodeI<T> prev = null;
  204. NodeI<T> current = head;
  205. NodeI<T> next;
  206.  
  207. //the loop that does the reverse itself
  208. while (null != current) {
  209. next = current.getNextReference();
  210. current.setNextReference(prev);
  211. prev = current;
  212. current = next;
  213. }
  214.  
  215. head = prev;
  216. }
  217.  
  218. @Override
  219. public boolean equals(Object o) {
  220. if (this == o) return true;
  221. if (o == null || getClass() != o.getClass()) return false;
  222. LinkedList<?> that = (LinkedList<?>) o;
  223. return Objects.equals(getData(), that.getData());
  224. }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement