LinkedList.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3. /**
  4. * A linked list of integers.
  5. */
  6. public class LinkedList implements Iterable<Integer> {
  7. // Main method - used for testing purposes
  8. public static void main(String[] args) {
  9. LinkedList list = new LinkedList();
  10. LinkedList list2 = new LinkedList();
  11. for (int i = 0; i < 10; i++) {
  12. int randInt = (int) (Math.random() * 100);
  13. list.add(randInt);
  14. list2.add(randInt);
  15. list.add(2);
  16. list2.add(2);
  17. }
  18. list.add(5);
  19. list.printList();
  20. System.out.println(list.countOccurrences(2));
  21. System.out.println(list.getIndex(5));
  22. System.out.println(list.equals(list2));
  23. }
  24. // Node objects store the list's elements
  25. private class Node {
  26. int element; // element stored in this node
  27. Node next; // link to next node in the list (or null)
  28. }
  29. private Node head; // Reference to first node in the list
  30. private Node tail; // Reference to last node in the list
  31. private int size; // Number of elements stored in the list
  32. /**
  33. * Default constructor - creates an empty list
  34. */
  35. public LinkedList() {
  36. head = tail = null;
  37. size = 0;
  38. }
  39. // Prints the whole list (driver method)
  40. public void printList() {
  41. printList(head);
  42. System.out.println();
  43. }
  44. // Print list starting at walker (worker method)
  45. private void printList(Node walker) {
  46. if (walker != null) {
  47. System.out.print(walker.element + " "); // print current element
  48. printList(walker.next); // print rest of list
  49. }
  50. }
  51. /**
  52. * Returns the size of this list.
  53. * @return size of list
  54. */
  55. public int size() {
  56. return size;
  57. }
  58. /**
  59. * Adds the given element to the end of the list.
  60. * @param element element to add
  61. */
  62. public void add(int element) {
  63. Node n = new Node();
  64. n.element = element;
  65. if (size == 0) {
  66. head = tail = n;
  67. } else {
  68. tail.next = n;
  69. tail = n;
  70. }
  71. size++;
  72. }
  73. /**
  74. * Removes the first occurrence of the given element from the list.
  75. * @param element element to remove
  76. * @throws NoSuchElementException if given element not in the list
  77. */
  78. public void remove(int element) {
  79. Node walker = head;
  80. Node follower = null;
  81. while (walker != null && walker.element != element) {
  82. follower = walker;
  83. walker = walker.next;
  84. }
  85. if (walker == null) {
  86. throw new NoSuchElementException();
  87. } else if (walker == head) {
  88. head = head.next;
  89. if (head == null) tail = null;
  90. } else if (walker == tail) {
  91. follower.next = null;
  92. tail = follower;
  93. } else {
  94. follower.next = walker.next;
  95. }
  96. size--;
  97. }
  98. /**
  99. * Returns the element in the list at the given index.
  100. * @param index index of element
  101. * @return element at given index
  102. * @throws IndexOutOfBoundsException if index invalid
  103. */
  104. public int get(int index) {
  105. if (index < 0 || index >= size) throw new IndexOutOfBoundsException(
  106. index + ""
  107. );
  108. Node walker = head;
  109. for (int i = 0; i < index; i++) walker = walker.next;
  110. return walker.element;
  111. }
  112. @Override
  113. /**
  114. * Returns an iterator object for this list.
  115. * @return iterator object
  116. */
  117. public Iterator<Integer> iterator() {
  118. return new LinkedListIterator();
  119. }
  120. // This inner class defines the iterator object
  121. // for this list.
  122. private class LinkedListIterator implements Iterator<Integer> {
  123. private Node walker; // Link to "next" node
  124. // Constructor - creates an iterator object that
  125. // starts at the head of the list.
  126. public LinkedListIterator() {
  127. walker = head;
  128. }
  129. @Override
  130. // Returns true if the iterator has not reached the
  131. // end of the list.
  132. public boolean hasNext() {
  133. return walker != null;
  134. }
  135. @Override
  136. // Returns the "next" element in the list and advances
  137. // the walker.
  138. public Integer next() {
  139. if (hasNext()) {
  140. Integer elem = walker.element;
  141. walker = walker.next;
  142. return elem;
  143. } else {
  144. throw new NoSuchElementException();
  145. }
  146. }
  147. }
  148. public int countOccurrences(int element) {
  149. return countOccurrences(element, head, 0);
  150. }
  151. private int countOccurrences(int element, Node walker, int count) {
  152. if (walker == null) return count;
  153. if (walker.element == element) count++;
  154. return countOccurrences(element, walker.next, count);
  155. }
  156. public int getIndex(int element) {
  157. return getIndex(element, head, 0);
  158. }
  159. private int getIndex(int element, Node walker, int index) {
  160. if (walker == null) return -1;
  161. else if (walker.element == element) return index;
  162. index++;
  163. return getIndex(element, walker.next, index);
  164. }
  165. public boolean equals(Object other) {
  166. if (other instanceof LinkedList) {
  167. LinkedList otherList = (LinkedList) other;
  168. if (size == otherList.size) return equals(head, otherList.head);
  169. else return false;
  170. } else return false;
  171. }
  172. private boolean equals(Node walkerA, Node walkerB) {
  173. if (walkerA == null || walkerB == null) return true;
  174. if (walkerA.element != walkerB.element) return false;
  175. return equals(walkerA.next, walkerB.next);
  176. }
  177. public LinkedList cloneList() {
  178. return cloneList(head, new LinkedList());
  179. }
  180. private LinkedList cloneList(Node walker, LinkedList newList) {
  181. if (walker == null) return newList;
  182. newList.add(walker.element);
  183. return cloneList(walker.next, newList);
  184. }
  185. }