Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. /*
  2. * Singly linked list
  3. */
  4. package linkedlisttest;
  5.  
  6. class Node {
  7.  
  8. int data;
  9. Node next;
  10.  
  11. public Node(int data) {
  12. this.data = data;
  13. }
  14.  
  15. }
  16.  
  17. class LinkedList {
  18.  
  19. Node head;
  20. int size;
  21.  
  22. /**
  23. *
  24. * @param data element to add to list
  25. * Time Complexity : O(n)
  26. */
  27. public void add(int data) {
  28. if (head == null) {
  29. head = new Node(data);
  30. size += 1;
  31. return;
  32. }
  33.  
  34. Node current = head;
  35. while (current.next != null) {
  36. current = current.next;
  37. }
  38. current.next = new Node(data);
  39. size += 1;
  40. }
  41.  
  42. /**
  43. *
  44. * @return size of list
  45. * Time Complexity: O(1)
  46. * This is because we use a class
  47. * variable size to keep track of size of linked list
  48. */
  49. public int getSize() {
  50. return size;
  51. }
  52. /**
  53. *
  54. * @param data element to insert
  55. * @param index position at which to insert the element (zero based)
  56. * Time Complexity : O(n)
  57. */
  58. public void add(int data, int index) {
  59.  
  60. if (index > getSize()) {
  61. return; // invalid position
  62. }
  63.  
  64. Node current = head; //iterate through whole list
  65. int pos = 0;
  66. Node newNode = new Node(data);
  67.  
  68. if (index == 0) // special case, since its a single reference change!
  69. {
  70. newNode.next = head;
  71. head = newNode; // this node is now the head
  72. size += 1;
  73. return;
  74. }
  75. while (current.next != null) {
  76. if (pos == index - 1) {
  77. break;
  78. }
  79. pos++;
  80. current = current.next;
  81. }
  82. // These are 2 reference changes, as compared to adding at index 0
  83. newNode.next = current.next; // here we are changing a refernce
  84. current.next = newNode; // changing a reference here as well
  85. size += 1;
  86.  
  87. }
  88. /**
  89. * Find the first occurrence of an element
  90. * @param data element to find
  91. * @return index at which element is found , -1 if not exist
  92. * Time Complexity: O(n)
  93. */
  94. public int find(int data) {
  95. Node current = head;
  96. int pos = 0;
  97. int index = -1;
  98. if(head == null) { //empty list
  99. return index;
  100. }
  101. while(current != null) {
  102. if (current.data == data) {
  103. index = pos;
  104. break;
  105. }
  106. pos++;
  107. current = current.next;
  108. }
  109. return index;
  110. }
  111.  
  112. /**
  113. * Delete the first occurrence of data
  114. * @param data element to delete
  115. * Time complexity : O(n)
  116. */
  117. public void delete(int data) {
  118. Node current = head;
  119. if (head == null) { // list is empty
  120. return;
  121. }
  122. if(head.data == data) { // if we want to delete the head , make next node head
  123. head = head.next;
  124. size -= 1;
  125. return;
  126. }
  127. while(current.next != null) {
  128. if (current.next.data == data) {
  129. current.next = current.next.next;
  130. size -= 1;
  131. return;
  132. }
  133. current = current.next;
  134. }
  135. }
  136.  
  137. /**
  138. * Delete all occurrences of data
  139. * @param data element to delete
  140. *
  141. */
  142. public void deleteAll(int data) {
  143. Node current = head;
  144. if (head == null) { // list is empty
  145. return;
  146. }
  147. //while loop to delete consecutive occurances of data
  148. while(head.data == data) { // if we want to delete the head , make next node head
  149. head = head.next;
  150. size -= 1;
  151. }
  152. while(current.next != null) {
  153. //while loop to delete consecutive occurances of data
  154. while (current.next.data == data) {
  155. current.next = current.next.next;
  156. size -= 1;
  157. }
  158. current = current.next;
  159. }
  160. }
  161.  
  162. public void reverse() {
  163.  
  164. }
  165.  
  166.  
  167.  
  168. /**
  169. * Prints the whole linked list
  170. * Time Complexity : O(n)
  171. */
  172. public void print() {
  173.  
  174. if(head == null) { //list is empty
  175. return;
  176. }
  177. Node current = head;
  178. while (current.next != null) {
  179. System.out.print(current.data + "->");
  180. current = current.next;
  181. }
  182. System.out.print(current.data + "n");
  183. }
  184. }
  185.  
  186. public class LinkedListTest {
  187.  
  188. /**
  189. * @param args the command line arguments
  190. */
  191. public static void main(String[] args) {
  192. LinkedList lt = new LinkedList();
  193. lt.print();
  194. lt.add(3);
  195. lt.add(5);
  196. lt.add(6);
  197. lt.print();
  198. lt.add(4, 1);
  199. lt.print();
  200. lt.add(4, 7);// 7 is an invalid index
  201. lt.add(8, 3);
  202. lt.add(8, 4);
  203. lt.print();
  204. System.out.println("Position : " + lt.find(8));
  205. lt.delete(5);
  206. lt.print();
  207. lt.deleteAll(8);
  208. lt.print();
  209. System.out.println("Size : " + lt.getSize());
  210. }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement