Advertisement
skilletwaffles

lab 31.1 as of march 30

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