Advertisement
skilletwaffles

lab 31.1

Mar 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 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. ListNode temp = first;
  114. while(temp != null)
  115. {
  116.  
  117. }
  118. }
  119.  
  120. /**
  121. * Returns the first occurrence of the specified element, or null
  122. * if the List does not contain this element.
  123. *
  124. * @param element element to search for.
  125. * @return first occurrence of the specified element, or null
  126. * if the list doesn not contain the element.
  127. */
  128. public Object find(Comparable element)
  129. {
  130. System.out.println("find: " + element);
  131. ListNode temp = first;
  132. ListNode answer = first;
  133.  
  134. while(temp!=null)
  135. {
  136. if(((Comparable)temp.getValue()).compareTo(element) ==0)
  137. {
  138. answer = temp;
  139. }
  140. temp= temp.getNext();
  141. }
  142. if(temp == null)
  143. answer = null;
  144. return answer;
  145.  
  146. }
  147.  
  148. /**
  149. * Removes the first occurrence of the specified element in
  150. * this list. If the list does not contain the element, it
  151. * is unchanged.
  152. *
  153. * @param element element to be removed from this list, if present.
  154. * @return removes first element with matching element, if any.
  155. */
  156. public Object remove(Object element)
  157. {
  158. System.out.println("remove: " + element);
  159. ListNode temp = first;
  160. ListNode prev = first;
  161. while(temp !=null)
  162. {
  163. if(((Comparable)temp.getValue()).compareTo(element)==0)
  164. {
  165. prev.setNext(temp.getNext());
  166. }
  167. }
  168. if(temp == null)
  169. {
  170. return null;
  171. }
  172. }
  173.  
  174. /**
  175. * Returns the number of elements in this list.
  176. *
  177. * @return number of elements in this list.
  178. */
  179. public int size()
  180. {
  181. int count = 0;
  182. ListNode temp = first;
  183. while(temp !=null)
  184. {
  185. count++;
  186. temp = temp.getNext();
  187. }
  188. return count;
  189. }
  190.  
  191. /**
  192. * Prints all the elements of the list
  193. */
  194. public void printList()
  195. {
  196. ListNode temp = first; // start from the first node
  197. while (temp != null)
  198. {
  199. System.out.println(((Item)temp.getValue()).getId() + " " +
  200. ((Item)temp.getValue()).getInv());
  201. temp = temp.getNext(); // go to next node
  202. }
  203. }
  204.  
  205. /**
  206. * Prints all the elements of the list in reverse order
  207. */
  208. public void printBackwards()
  209. {
  210. printBackwards(first);
  211. }
  212.  
  213. /**
  214. * Recursive helper method to print all the elements of
  215. * the list in reverse order
  216. */
  217. private void printBackwards(ListNode temp)
  218. {
  219. System.out.println("printBackwards");
  220. }
  221. /**
  222. * Removes all of the elements from this list.
  223. */
  224. public void clear()
  225. {
  226. first = null;
  227. last = null;
  228. }
  229.  
  230. /**
  231. * Returns a string representation of this list. The string
  232. * representation consists of the list's elements in order,
  233. * enclosed in square brackets ("[]"). Adjacent elements are
  234. * separated by the characters ", " (comma and space).
  235. *
  236. * @return Description of the Returned Value
  237. */
  238. public String toString()
  239. {// post: returns a string representing list
  240.  
  241. String s = "[";
  242.  
  243. ListNode temp = first; // start from the first node
  244. while (temp != null)
  245. {
  246. s += temp.getValue() + ", "; // append the data
  247. temp = temp.getNext(); // go to next node
  248. }
  249. s += "]";
  250. return s;
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement