Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class LinkedList {
  4. public Node head;
  5. public Node tail;
  6.  
  7. public LinkedList() {
  8. head = null;
  9. tail = null;
  10. }
  11.  
  12. public int[] toArray() {
  13. int count = this.count();
  14. int[] arr = new int[count];
  15. Node node = this.head;
  16. for (int i = 0; i < arr.length; i++) {
  17. arr[i] = node.value;
  18. node = node.next;
  19. }
  20. return arr;
  21. }
  22.  
  23. public void addInTail(Node item) {
  24. if (this.head == null)
  25. this.head = item;
  26. else
  27. this.tail.next = item;
  28. this.tail = item;
  29. }
  30.  
  31. public Node find(int value) {
  32. Node node = this.head;
  33. while (node != null) {
  34. if (node.value == value)
  35. return node;
  36. node = node.next;
  37. }
  38. return null;
  39. }
  40.  
  41. public ArrayList<Node> findAll(int _value) {
  42. ArrayList<Node> nodes = new ArrayList<>();
  43. Node node = this.head;
  44. while (node != null) {
  45. if (node.value == _value) {
  46. nodes.add(node);
  47. }
  48. node = node.next;
  49. }
  50.  
  51. return nodes;
  52. }
  53.  
  54. public boolean remove(int _value) {
  55. if (this.head == null) {
  56. return false;
  57. }
  58.  
  59. Node node = this.head;
  60. Node prevNode = null;
  61.  
  62. boolean isNodeFound = false;
  63. while (node != null) {
  64. if (node.value == _value) {
  65. isNodeFound = true;
  66. break;
  67. }
  68.  
  69. prevNode = node;
  70. node = node.next;
  71. }
  72.  
  73. if (isNodeFound) {
  74. if (node == tail) {
  75. tail = prevNode;
  76. }
  77.  
  78. if (node == head) {
  79. head = node.next;
  80. }
  81.  
  82. if (prevNode != null) {
  83. prevNode.next = node.next;
  84. }
  85. node = null;
  86. }
  87.  
  88. return isNodeFound;
  89. }
  90.  
  91. public void removeAll(int _value) {
  92.  
  93. if (this.head == null) {
  94. return;
  95. }
  96.  
  97. Node node = this.head;
  98. Node prevNode = null;
  99.  
  100. while (node != null) {
  101. if (node.value == _value) {
  102. if (node == this.head) {
  103. this.head = node.next;
  104. }
  105.  
  106. if (node == this.tail) {
  107. this.tail = prevNode;
  108. }
  109.  
  110. if (prevNode != null) {
  111. prevNode.next = node.next;
  112. node = prevNode;
  113. }
  114. }
  115. prevNode = node;
  116. node = node.next;
  117. }
  118. }
  119.  
  120. public void clear() {
  121. this.head = null;
  122. this.tail = null;
  123. }
  124.  
  125. public int count() {
  126. Node node = this.head;
  127. int cnt = 0;
  128.  
  129. if (node != null) {
  130. while (node != null) {
  131. cnt++;
  132. node = node.next;
  133. }
  134. }
  135. return cnt;
  136. }
  137.  
  138. public void insertAfter(Node _nodeAfter, Node _nodeToInsert) {
  139. if (this.head == null || _nodeAfter == this.tail) {
  140. addInTail(_nodeToInsert);
  141. } else if (_nodeAfter == null) {
  142. _nodeToInsert.next = this.head;
  143. this.head = _nodeToInsert;
  144. } else {
  145. _nodeToInsert.next = _nodeAfter.next;
  146. _nodeAfter.next = _nodeToInsert;
  147. }
  148. }
  149.  
  150. }
  151.  
  152. class Node {
  153. public int value;
  154. public Node next;
  155.  
  156. public Node(int _value) {
  157. value = _value;
  158. next = null;
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement