Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. public class LinkedList {
  2. private Node front;
  3. //private int count;
  4. //private Node end;
  5. public LinkedList(Node f) {
  6. front = f;
  7. }
  8. public LinkedList() {
  9. front = null;
  10. }
  11. public void addToFront(String d) {
  12. Node n = new Node(d, front);
  13. front = n;
  14. }
  15. public boolean isEmpty() {
  16. if(front == null)
  17. return true;
  18. else
  19. return false;
  20. }
  21. public void clear() {
  22. front = null;
  23. }
  24. public String getFrontData() {
  25. return front.getData();
  26. }
  27. public Node getFrontNode() {
  28. return front;
  29. }
  30. public String toString() {
  31. String ts = "[";
  32. Node cur = front;
  33. while(cur != null) {
  34. ts += cur;
  35. cur = cur.getNext();
  36. }
  37. return ts + "]";
  38. }
  39. public int size() {
  40. int count = 0;
  41. Node cur = front;
  42. while(cur != null) {
  43. count++;
  44. cur = cur.getNext();
  45. }
  46. return count;
  47. }
  48. public void removeFront() {
  49. if(!isEmpty())
  50. front = front.getNext();
  51. // else
  52. // System.out.println(“No front to remove!”);
  53. }
  54. public void addToEnd(String d) {
  55. Node n = new Node(d, null);
  56. if(isEmpty())
  57. front = n;
  58. else {
  59. Node cur = front;
  60. while(cur.getNext() != null)
  61. cur = cur.getNext();
  62. cur.setNext(n);
  63. }
  64. }
  65. public void removeLast() {
  66. if(!isEmpty()) {
  67. if(front.getNext() == null)
  68. front = null;
  69. else {
  70. Node cur = front;
  71. while(cur.getNext().getNext() != null)
  72. cur = cur.getNext();
  73. cur.setNext(null);
  74. }
  75. //} else { System.out.println(“No end to remove!”);
  76. }
  77. }
  78. public int contains(String d) {
  79. Node cur = front;
  80. boolean found = false;
  81. int index = -1;
  82. while(cur != null && !found) {
  83. index++;
  84. if(cur.getData().equals(d))
  85. found = true;
  86. cur = cur.getNext();
  87. }
  88. if(!found)
  89. index = -1;
  90. return index;
  91. }
  92. public void add(int index, String d) {
  93. if(index >= 0 && index <= size() ) {
  94. if(index == 0)
  95. addToFront(d);
  96. else {
  97. Node cur = front;
  98. for(int i=0; i<index-1; i++)
  99. cur = cur.getNext();
  100. Node n = new Node(d, cur.getNext());
  101. cur.setNext(n);
  102. }
  103. }
  104. //else { System.out.println(“Index out of bounds!”); }
  105. }
  106. public void remove(int index) {
  107. if(index >= 0 && index <= size()) {
  108. if(index == 0)
  109. removeFront();
  110. else if(index == size()-1)
  111. removeLast();
  112. else {
  113. Node cur = front;
  114. for(int i=0; i<index-1; i++)
  115. cur = cur.getNext();
  116. cur.setNext(cur.getNext().getNext());
  117. }
  118. }
  119. //else { System.out.println(“Index out of bounds!”); }
  120. }
  121. public Node getNode(int index) {
  122. Node cur = null;
  123. if(index >= 0 && index <= size()) {
  124. if(index == 0)
  125. cur = front;
  126. else {
  127. cur = front;
  128. for(int i=0; i<index; i++)
  129. cur = cur.getNext();
  130. }
  131. } // else { System.out.println(“Index out of bounds!”); }
  132. return cur;
  133. }
  134. public void addAll(LinkedList other) {
  135. Node cur = other.getFrontNode();
  136. while(cur != null) {
  137. addToEnd(cur.getData());
  138. cur = cur.getNext();
  139. }
  140. }
  141. public static LinkedList merge(LinkedList first, LinkedList second) {
  142. LinkedList result = new LinkedList();
  143. Node cur = first.getFrontNode();
  144. while(cur != null) {
  145. result.addToEnd(cur.getData());
  146. cur = cur.getNext();
  147. }
  148. cur = second.getFrontNode();
  149. while(cur != null) {
  150. result.addToEnd(cur.getData());
  151. cur = cur.getNext();
  152. }
  153. return result;
  154. }
  155. public LinkedList subList(int start, int end) {
  156. LinkedList result = new LinkedList();
  157. if(!(start >= end || start < 0 || start > size() || end >= size() )) {
  158. Node cur = getFrontNode();
  159. for(int i=0; i<start; i++) {
  160. cur = cur.getNext();
  161. }
  162. for(int i=start; i<=end; i++) {
  163. addToEnd(cur.getData());
  164. cur = cur.getNext();
  165. }
  166. }
  167. return result;
  168. }
  169. public static LinkedList union(LinkedList first, LinkedList second) {
  170. LinkedList result = new LinkedList();
  171. Node cur = first.getFrontNode();
  172. while(cur != null) {
  173. result.addToEnd(cur.getData());
  174. cur = cur.getNext();
  175. }
  176. cur = second.getFrontNode();
  177. while(cur != null) {
  178. if(result.contains(cur.getData()) == -1)
  179. result.addToEnd(cur.getData());
  180. cur = cur.getNext();
  181. }
  182. return result;
  183. }
  184. public static LinkedList intersection(LinkedList first, LinkedList second) {
  185. LinkedList result = new LinkedList();
  186. Node cur = first.getFrontNode();
  187. while(cur != null) {
  188. if(second.contains(cur.getData()) != -1)
  189. result.addToEnd(cur.getData());
  190. cur = cur.getNext();
  191. }
  192. return result;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement