Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. class myLinkedList {
  2. private static class Node
  3. {
  4. private int data;
  5. private Node next;
  6.  
  7. public Node(int data, Node next)
  8. {
  9. this.data = data;
  10. this.next = next;
  11. }
  12. }
  13.  
  14. private Node head;
  15.  
  16. // Constructs an empty list
  17. public myLinkedList()
  18. {
  19. head = null;
  20. }
  21.  
  22. // Returns true if the list is empty otherwise returns false
  23. public boolean isEmpty()
  24. {
  25. return (head == null);
  26. }
  27.  
  28. // Inserts a new node at the beginning of this list.
  29. public void insertAtBeginning(int element)
  30. {
  31. head = new Node(element, head);
  32. }
  33.  
  34. // Returns the first element in the list.
  35. public int getFirstElement()
  36. {
  37. if(head == null)
  38. {
  39. System.out.println("Empty linked list");
  40. throw new IndexOutOfBoundsException();
  41. }
  42. return head.data;
  43. }
  44.  
  45. // Removes the first node in the list.
  46. public int removeFirstNode()
  47. {
  48. int tmp = getFirstElement();
  49. head = head.next;
  50. return tmp;
  51. }
  52.  
  53. // Empties linked list
  54. public void clear()
  55. {
  56. head = null;
  57. }
  58.  
  59. // Returns the length of the linked list
  60. public static int LLlength(Node head)
  61. {
  62. int length = 0;
  63. Node currentNode = head;
  64.  
  65. while(currentNode != null)
  66. {
  67. length++;
  68. currentNode = currentNode.next;
  69. }
  70. return length;
  71. }
  72.  
  73. // Displays the linked list elements
  74. public static void display(Node head)
  75. {
  76. if(head == null)
  77. {
  78. System.out.println("Empty linked list");
  79. throw new IndexOutOfBoundsException();
  80. }
  81.  
  82. Node currentNode = head;
  83.  
  84. while(currentNode != null)
  85. {
  86. System.out.print(currentNode.data+" ");
  87. currentNode = currentNode.next;
  88. }
  89. System.out.println();
  90. }
  91.  
  92. // Displays the linked list elements in reverse order
  93. public static void displayReverse(Node head)
  94. {
  95. if(head == null){}
  96. else
  97. {
  98. Node currentNode = head;
  99. displayReverse(currentNode.next);
  100. System.out.print(currentNode.data+" ");
  101. }
  102. }
  103. //Displays the linked list's last element
  104. public static int getLastElement(Node head)
  105. {
  106. Node currentNode = head;
  107.  
  108. while(currentNode.next != null)
  109. {
  110. currentNode = currentNode.next;
  111. }
  112. return currentNode.data;
  113. }
  114. public static void insertAtLast(Node head,int element)
  115. {
  116. Node newNode=null;
  117. newNode.data = element;
  118. newNode.next = null;
  119. while(head.next != null)
  120. {
  121. head = head.next;
  122. }
  123. head = newNode;
  124. //return head;
  125.  
  126. }
  127.  
  128. //Tells if a sepeific element is in the Linked List or not
  129. public static boolean searchFor(Node head, int element)
  130. {
  131. Node currentNode = head;
  132. boolean flag = false;
  133. while(currentNode != null)
  134. {
  135. if (currentNode.data == element)
  136. {
  137. flag = true;
  138. break;
  139. }
  140. currentNode = currentNode.next;
  141. }
  142. return flag;
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement