Advertisement
Guest User

Untitled

a guest
Sep 19th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. /**
  2. * Sorts a list of nodes with generic type.
  3. * @author John Doe
  4. * @param <T> user defined type
  5. */
  6.  
  7. public class OrderedList<T extends Comparable<T>> {
  8. private Node<T> head;
  9. private int size;
  10.  
  11. /**
  12. * Initializes a new ordered list object.
  13. */
  14. public OrderedList() {
  15. head = null;
  16. size = 0;
  17. }
  18.  
  19. /**
  20. * Creates a new node with a given item and inserts it into its correct
  21. * position in the list so that the list is in ascending order.
  22. * @param element newly inserted element
  23. */
  24. public void insert(T element) {
  25. Node<T> newNode = new Node<>(element, null);
  26. if (head == null) {
  27. head = newNode;
  28. } else {
  29. Node<T> current = head;
  30. Node<T> previous = null;
  31. while (current != null && current.data.compareTo(element) < 0) {
  32. previous = current;
  33. current = current.next;
  34. }
  35. newNode.next = current;
  36. if (previous == null) {
  37. head = newNode;
  38. } else {
  39. previous.next = newNode;
  40. }
  41. }
  42. size++;
  43. }
  44.  
  45. /**
  46. * Returns a copy of the k-th item in the list for a given integer k.
  47. * @param index the position the user specifies
  48. * @return The element the user wants to get
  49. * @throws OutOfBoundException if the index is out of bounds
  50. */
  51. public T get(int index) throws OutOfBoundException
  52. {
  53. if (index < 0 || index >= size)
  54. {
  55. throw new OutOfBoundException("Index: " + index + " size: " + size);
  56. }
  57. Node<T> current = head;
  58. for (int i = 0; i < index; i++) {
  59. current = current.next;
  60. }
  61. return current.data;
  62. }
  63.  
  64. /**
  65. * Removes the first instance of a user-specified item.
  66. * @param item user specified item
  67. */
  68. public void remove(T item)
  69. {
  70. Node<T> current = head;
  71. Node<T> previous = null;
  72. while (current != null) {
  73. if (item.equals(current.data)) {
  74. if (previous == null) {
  75. head = current.next;
  76. } else {
  77. previous.next = current.next;
  78. }
  79. size--;
  80. } else {
  81. previous = current;
  82. }
  83. current = current.next;
  84. }
  85. }
  86.  
  87. /**
  88. * Returns the last k elements in this ordered list.
  89. * @param k number of elements to return
  90. * @return List of the k largest elements
  91. * @throws OutOfBoundException if the index is out of bounds
  92. */
  93. public OrderedList kLargest(int k) throws OutOfBoundException
  94. {
  95. if (k >= size || k < 0)
  96. k = Math.abs(size);
  97. OrderedList ol = new OrderedList();
  98. for (int i = size - k; i < size; i++)
  99. {
  100. ol.insert(get(i));
  101. }
  102. return ol;
  103. }
  104.  
  105. /**
  106. * Temporary, crappy method doing what add(OrderedList list2) should do;
  107. * it should traverse through the nodes of this list and the other nodes
  108. * exactly one without having to repeatedly search the location where the
  109. * items should be added to on the new list.
  110. * @param list2 another ordered list
  111. * @return Elements from both ordered list
  112. * @throws OutOfBoundException if the index is out of bounds
  113. */
  114. public OrderedList addFrom(OrderedList list2) throws OutOfBoundException
  115. {
  116. OrderedList list3 = new OrderedList();
  117. for (int i = 0; i < size; i++)
  118. {
  119. list3.insert(get(i));
  120. }
  121. for (int j = 0; j < list2.size; j++)
  122. {
  123. T element = (T) list2.get(j);
  124. list3.insert(element);
  125. }
  126. return list3;
  127. }
  128.  
  129. /**
  130. * Method of implementing proper version of "addFrom" should do with proper
  131. * efficiency.
  132. * @param list2 another ordered list
  133. * @return Elements from both ordered list
  134. * @throws OutOfBoundException if the index is out of bounds
  135. */
  136. public OrderedList add(OrderedList list2) throws OutOfBoundException
  137. {
  138. int i = 0;
  139. int j = 0;
  140. OrderedList list3 = new OrderedList();
  141. int k = i + j;
  142. while (k < this.size + list2.size)
  143. {
  144. if(list2.get(i).compareTo(this.get(j)) < 0)
  145. {
  146. System.out.println(list2.get(i));
  147. list3.insert(list2.get(i));
  148. i++;
  149. }
  150. else
  151. {
  152. System.out.println(this.get(j));
  153. list3.insert(this.get(j));
  154. j++;
  155. }
  156. }
  157. return list3;
  158. }
  159.  
  160. /**
  161. * Returns a neatly formatted list with every node.
  162. * @return A representation of the nodes in the list
  163. */
  164. @Override
  165. public String toString()
  166. {
  167. String st = "[";
  168. for (Node<T> n = head; n != null; n = n.next)
  169. st += n.data + ((n.next == null ? "]" : ", "));
  170. return st;
  171. }
  172. }
  173.  
  174. /**
  175. * Establishes that a node object has data and a next node object.
  176. * @author John Doe
  177. * @param <T> user defined type
  178. */
  179. class Node<T> {
  180. T data;
  181. Node<T> next;
  182.  
  183. /**
  184. * Initializes a new node object.
  185. * @param data specified user input
  186. * @param next what is the node next to
  187. */
  188. public Node(T data, Node<T> next) {
  189. this.data = data;
  190. this.next = next;
  191. }
  192. }
  193.  
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement