Advertisement
skilletwaffles

lab 32.1

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