skilletwaffles

lab 31.1 final

Apr 1st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. import java.util.*;
  2. import apcslib.*;
  3. import chn.util.*;
  4.  
  5. /**
  6. * Implementation of lists, using singly linked elements.
  7. *
  8. * @author G. Peck
  9. * @created April 27, 2002
  10. */
  11. public class SinglyLinkedList
  12. {
  13. private ListNode first; // first element
  14. private ListNode last; // last elementt
  15.  
  16. /**
  17. * Constructor for the SinglyLinkedList object
  18. * Generates an empty list.
  19. */
  20. public SinglyLinkedList()
  21. {
  22. first = last = null;
  23. }
  24.  
  25. /**
  26. * Returns true if this list contains no elements.
  27. *
  28. * @return true iff the list is empty
  29. */
  30. public boolean isEmpty()
  31. {
  32. if(first == null)
  33. return true;
  34. else
  35. return false;
  36. }
  37.  
  38. /**
  39. * Returns the first element in this list.
  40. *
  41. * @return the first element in the linked list.
  42. */
  43. public Object getFirst()
  44. {
  45. if (first == null)
  46. {
  47. throw new NoSuchElementException();
  48. }
  49. else
  50. return first.getValue();
  51. }
  52.  
  53. /**
  54. * Inserts the given element at the beginning of this list.
  55. *
  56. * @param element the element to be inserted at the beginning of this list.
  57. */
  58. public void addFirst(Object element)
  59. {
  60. // note the order that things happen:
  61. // head is parameter, then assigned
  62. first = new ListNode(element, first);
  63. if (last == null)
  64. {
  65. last = first;
  66. }
  67. }
  68.  
  69. /**
  70. * Returns the last element in this list.
  71. *
  72. * @return the last element in the linked list.
  73. */
  74. public Object getLast()
  75. {
  76. if (last == null)
  77. {
  78. throw new NoSuchElementException();
  79. }
  80. else
  81. return last.getValue();
  82. }
  83.  
  84. /**
  85. * Adds and object to the end of the list
  86. *
  87. * @param element adds element to end of list
  88. */
  89. public void addLast(Object element)
  90. {
  91. if (first == null)
  92. {
  93. first = last = new ListNode(element, first);
  94. }
  95. else
  96. {
  97. last.setNext(new ListNode(element, null));
  98. last = last.getNext();
  99. }
  100. }
  101.  
  102. /**
  103. * Inserts the specified element at the position in this list
  104. * according to the natural ordering of its elements. All elements
  105. * in the list must implement the Comparable interface. Shifts
  106. * the element currently at that position (if any) and any
  107. * subsequent elements to the right.
  108. *
  109. * @param element element to be inserted
  110. */
  111. public void insert(Comparable element)
  112. {
  113. if(first == null)
  114. {first = last = new ListNode(element, first);
  115. }
  116.  
  117. ListNode temp = first.getNext();
  118. ListNode prev = first;
  119.  
  120. while(temp != null && element.compareTo(temp.getValue())< 0)
  121. {
  122. prev = temp;
  123. temp = temp.getNext();
  124. }
  125. ListNode inserted = new ListNode(element, temp);
  126. inserted.setNext(temp);
  127. prev.setNext(inserted);
  128. System.out.println("insert: " + element);
  129. }
  130.  
  131. /**
  132. * Returns the first occurrence of the specified element, or null
  133. * if the List does not contain this element.
  134. *
  135. * @param element element to search for.
  136. * @return first occurrence of the specified element, or null
  137. * if the list doesn not contain the element.
  138. */
  139. public Object find(Comparable element)
  140. {
  141. if(first == null)
  142. {
  143. return null;
  144. }
  145. ListNode temp = first;
  146. ListNode answer = first;
  147.  
  148. while(temp!=null && element.compareTo(temp.getValue()) !=0)
  149. {
  150. temp= temp.getNext();
  151. if(element.compareTo(temp.getValue()) == 0)
  152. {
  153. return temp.getValue();
  154. }
  155. }
  156. return null;
  157.  
  158. }
  159.  
  160. /**
  161. * Removes the first occurrence of the specified element in
  162. * this list. If the list does not contain the element, it
  163. * is unchanged.
  164. *
  165. * @param element element to be removed from this list, if present.
  166. * @return removes first element with matching element, if any.
  167. */
  168. public Object remove(Object element)
  169. {
  170. if(first == null)
  171. {
  172. return null;
  173. }
  174. ListNode temp = first.getNext();
  175. ListNode prev = first;
  176.  
  177. while(temp!=null && ((Comparable)temp.getValue()).compareTo(element) != 0)
  178. {
  179. prev = temp;
  180. temp = temp.getNext();
  181. }
  182. if(temp == null)
  183. {
  184. return null;
  185. }
  186.  
  187. if (((Comparable)element).compareTo(temp.getValue())==0)
  188. {
  189. prev.setNext(temp.getNext());
  190. return element;
  191. }
  192. else
  193. return null;
  194.  
  195.  
  196. }
  197.  
  198. /**
  199. * Returns the number of elements in this list.
  200. *
  201. * @return number of elements in this list.
  202. */
  203. public int size()
  204. {
  205. int count = 0;
  206. ListNode temp = first;
  207. while(temp !=null)
  208. {
  209. count++;
  210. temp = temp.getNext();
  211. }
  212. return count;
  213. }
  214.  
  215. /**
  216. * Prints all the elements of the list
  217. */
  218. public void printList()
  219. {
  220. ListNode temp = first; // start from the first node
  221. while (temp != null)
  222. {
  223. System.out.println(((Item)temp.getValue()).getId() + " " +
  224. ((Item)temp.getValue()).getInv());
  225. temp = temp.getNext(); // go to next node
  226. }
  227. }
  228.  
  229. /**
  230. * Prints all the elements of the list in reverse order
  231. */
  232. public void printBackwards()
  233. {
  234. printBackwards(first);
  235. }
  236.  
  237. /**
  238. * Recursive helper method to print all the elements of
  239. * the list in reverse order
  240. */
  241. private void printBackwards(ListNode temp)
  242. {
  243. if(temp==null)
  244. return;
  245. printBackwards(temp.getNext());
  246. System.out.println(((Item)temp.getValue()).getId() + " " +
  247. ((Item)temp.getValue()).getInv());
  248. }
  249.  
  250. /**
  251. * Removes all of the elements from this list.
  252. */
  253. public void clear()
  254. {
  255. first = null;
  256. last = null;
  257. }
  258.  
  259. /**
  260. * Returns a string representation of this list. The string
  261. * representation consists of the list's elements in order,
  262. * enclosed in square brackets ("[]"). Adjacent elements are
  263. * separated by the characters ", " (comma and space).
  264. *
  265. * @return Description of the Returned Value
  266. */
  267. public String toString()
  268. {// post: returns a string representing list
  269.  
  270. String s = "[";
  271.  
  272. ListNode temp = first; // start from the first node
  273. while (temp != null)
  274. {
  275. s += temp.getValue() + ", "; // append the data
  276. temp = temp.getNext(); // go to next node
  277. }
  278. s += "]";
  279. return s;
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment