skilletwaffles

lab 30.1

Mar 24th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. /**
  2. * Implementation of a node of a singly linked list.
  3. *
  4. * Adapted from the College Board's AP Computer Science AB:
  5. * Implementation Classes and Interfaces.
  6. */
  7. public class ListNode
  8. {
  9. private Object value;
  10. private ListNode next;
  11.  
  12. /**
  13. * Constructs a new element with object initValue,
  14. * followed by next element
  15. *
  16. * @param initValue New element object
  17. * @param initNext Reference to next element
  18. */
  19. public ListNode(Object initValue, ListNode initNext)
  20. {
  21. value = initValue;
  22. next = initNext;
  23. }
  24.  
  25. /**
  26. * Constructs a new tail of a list with object initValue
  27. *
  28. * @param initValue New element object
  29. */
  30. public ListNode(Object initValue)
  31. {
  32. this(initValue, null);
  33. }
  34.  
  35. /**
  36. * Sets the value attribute of the ListNode object
  37. *
  38. * @param theNewValue value attribute of the ListNode object
  39. */
  40. public void setValue(Object theNewValue)
  41. {
  42. value = theNewValue;
  43. }
  44.  
  45. /**
  46. * Sets reference to new next value
  47. *
  48. * @param theNewNext The new next value
  49. */
  50. public void setNext(ListNode theNewNext)
  51. {
  52. next = theNewNext;
  53. }
  54.  
  55. /**
  56. * Returns value associated with this element
  57. *
  58. * @return The value associated with this element
  59. */
  60. public Object getValue()
  61. {
  62. return value;
  63. }
  64.  
  65. /**
  66. * Returns reference to next value in list
  67. *
  68. * @return The next value in the list
  69. */
  70. public ListNode getNext()
  71. {
  72. return next;
  73. }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /**
  80. * Demostrates the use of the SinglyLinkedList class.
  81. *
  82. * @author G. Peck
  83. * @created May 12, 2002
  84. */
  85. public class ListDemo
  86. {
  87. SinglyLinkedList myList;
  88.  
  89. public ListDemo()
  90. {
  91. myList = new SinglyLinkedList();
  92. }
  93.  
  94. /**
  95. * Creates a SinglyLinkedList of 5 Integer nodes
  96. */
  97. public void createList()
  98. {
  99. for (int k = 1; k <= 20; k++)
  100. {
  101. myList.addFirst(new Integer(k));
  102. }
  103. }
  104.  
  105. /**
  106. * Display the first element of the list
  107. */
  108. public void displayFirst()
  109. {
  110. System.out.println("First Element: " + myList.getFirst());
  111. }
  112.  
  113. public void displayLast()
  114. {
  115. System.out.println("Last Element: " + myList.getLast());
  116. }
  117.  
  118. /**
  119. * Print the contents of myList
  120. */
  121. public void print()
  122. {
  123. myList.printList();
  124. System.out.println();
  125. }
  126.  
  127. /**
  128. * Demostrates the use of the SinglyLinkedList class.
  129. * Creates and prints a list of 5 consecutive Integer ojects.
  130. *
  131. * @param args The command line arguments (not used)
  132. */
  133. public static void main(String[] args)
  134. {
  135. ListDemo sList = new ListDemo();
  136.  
  137. sList.createList();
  138. sList.displayFirst();
  139. sList.print();
  140. sList.displayLast();
  141. }
  142. }
  143.  
  144.  
  145. import java.util.*;
  146.  
  147. /**
  148. * Implementation of lists, using singly linked elements.
  149. *
  150. * @author G. Peck
  151. * @created April 27, 2002
  152. */
  153. public class SinglyLinkedList
  154. {
  155. private ListNode first; // first element
  156. private ListNode last;
  157.  
  158. /**
  159. * Constructor for the SinglyLinkedList object
  160. * Generates an empty list.
  161. */
  162. public SinglyLinkedList()
  163. {
  164. first = null;
  165. }
  166.  
  167. /**
  168. * Returns the first element in this list.
  169. *
  170. * @return the first element in the linked list.
  171. */
  172. public Object getFirst()
  173. {
  174. if (first == null)
  175. {
  176. throw new NoSuchElementException();
  177. }
  178. else
  179. return first.getValue();
  180. }
  181.  
  182. /**
  183. * Inserts the given element at the beginning of this list.
  184. *
  185. * @param value the element to be inserted at the beginning of this list.
  186. */
  187. public void addFirst(Object value)
  188. {
  189. // note the order that things happen:
  190. // head is parameter, then assigned
  191. first = new ListNode(value, first);
  192. }
  193.  
  194. public void addLast(Object value)
  195. {
  196. if(first == null)
  197. {
  198. first = new ListNode(value, first.getNext());
  199. last = new ListNode(value, first.getNext());
  200. }
  201. else
  202. last = new ListNode(value, last.getNext());
  203. }
  204.  
  205. public Object getLast()
  206. {
  207. return last.getValue();
  208.  
  209. }
  210.  
  211. /**
  212. * Print the contents of the entire linked list
  213. */
  214. public void printList()
  215. {
  216. ListNode temp = first;// start from the first node
  217. while (temp != null)
  218. {
  219. System.out.print(temp.getValue() + " ");
  220. temp = temp.getNext();// go to next node
  221. }
  222. }
  223.  
  224. /**
  225. * Returns a string representation of this list. The string
  226. * representation consists of the list's elements in order,
  227. * enclosed in square brackets ("[]"). Adjacent elements are
  228. * separated by the characters ", " (comma and space).
  229. *
  230. * @return string representation of this list
  231. */
  232. public String toString()
  233. {
  234. String s = "[";
  235.  
  236. ListNode temp = first; // start from the first node
  237. while (temp != null)
  238. {
  239. s += temp.getValue(); // append the data
  240. temp = temp.getNext(); // go to next node
  241. if (temp != null)
  242. s += ", ";
  243. }
  244. s += "]";
  245. return s;
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment