Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. /**
  2. * lab goals: - Learn all about "do it yourself" linked lists
  3. * - Have a lot of fun looking at the bright colors red and green!
  4. *
  5. * class invariant: elements stored in the list are all of the same type.
  6. */
  7. import java.util.*;
  8. import java.io.PrintStream;
  9. import java.lang.Comparable;
  10. import java.lang.Object;
  11. import java.lang.String;
  12. public class SinglyLinkedList
  13. {
  14. private ListNode first;
  15. private Class type;
  16.  
  17. public SinglyLinkedList()
  18. {
  19. first = null;
  20. }
  21.  
  22. private void assertValidType(Object o)
  23. {
  24. if(type == null) type = o.getClass();
  25. else if(type!=o.getClass()) throw new IllegalArgumentException();
  26. }
  27.  
  28. /*
  29. * returns a string of the form: [obj.toString(), obj.toString(), obj.toString(), ..., obj.toString()]
  30. *
  31. * returns [] if the List is empty
  32. */
  33. public String toString()
  34. {
  35. if (first == null) return "[]";
  36. String returnItem = "";
  37. ListNode temperature = first;
  38. while (temperature != null)
  39. {
  40. returnItem += " " + temperature.getValue().toString() + ",";
  41. temperature = temperature.getNext();
  42. }
  43. return "[" + returnItem.substring(1,returnItem.length()-1)+"]";
  44. }
  45.  
  46. /* insert obj at index ind, shift all previous objects to the next higher index
  47. * e.g., list before: [1, 2, 3, 4]
  48. * list.add(2, 11) modifies list to [1, 2, 11, 3, 4]
  49. *
  50. * preCondition: 0 <= ind <= size()
  51. */
  52. public void add(int ind, Object obj)
  53. {
  54. int i = 0;
  55. ListNode back = null;
  56. ListNode yuh = first;
  57. while (i < ind)
  58. {
  59. i++;
  60. back = yuh;
  61. yuh = yuh.getNext();
  62. }
  63. if (back == null)
  64. first = new ListNode(obj, first);
  65. else
  66. back.setNext(new ListNode(obj, yuh));
  67. }
  68.  
  69. /*
  70. * return a referrence (not the object) to the obj at index ind.
  71. * list is not modified!
  72. * preCondition: 0 <= ind < size()
  73. * if list is empty return null
  74. */
  75. public Object get(int ind)
  76. {
  77. if (size() == 0)
  78. return null;
  79. int i = 0;
  80. ListNode yuh = first;
  81. while (i < ind)
  82. {
  83. i++;
  84. yuh = yuh.getNext();
  85. }
  86. return yuh.getValue();
  87. }
  88.  
  89. /*
  90. * return a referrence to the ListNode (not the object or Value) at index ind.
  91. * list is not modified!
  92. * preCondition: 0 <= ind < size()
  93. * if list is empty return null
  94. */
  95. public ListNode getNodeAtIndex(int ind)
  96. {
  97. if (size() == 0)
  98. return null;
  99. int i = 0;
  100. ListNode yuh = first;
  101. while (i < ind)
  102. {
  103. i++;
  104. yuh = yuh.getNext();
  105. }
  106. return yuh;
  107. }
  108.  
  109. /*
  110. * returns the number of elements in the List
  111. *
  112. * returns zero if the List is empty
  113. */
  114. public int size()
  115. {
  116. int num = 0;
  117.  
  118. ListNode yuh = first;
  119. while (yuh !=null)
  120. {
  121. num++;
  122. yuh = yuh.getNext();
  123. }
  124. return num;
  125. }
  126.  
  127. /*
  128. * returns the number of elements in the List from curNode, including curNode
  129. *
  130. * returns zero if the List is empty
  131. *
  132. * preCondition: curNode is a ListNode in List
  133. */
  134. public int sizeFrom(ListNode curNode)
  135. {
  136. if (curNode == null) return 0;
  137. return 1 + sizeFrom(curNode.getNext());
  138. }
  139.  
  140. /*
  141. * returns true if a ListNode in List is equal ( getValue().equals( o ) == true)
  142. *
  143. * returns false otherwise
  144. */
  145. public boolean contains(Object o)
  146. {
  147. ListNode yuh = first;
  148. while (yuh != null)
  149. {
  150. if (yuh.getValue().equals(o)) return true;
  151. yuh = yuh.getNext();
  152. }
  153. return false;
  154. }
  155.  
  156. /*
  157. * returns true if the size of the List is 0
  158. *
  159. * Returns false otherwise
  160. */
  161. public boolean isEmpty()
  162. {
  163. return first == null;
  164. }
  165.  
  166. /*
  167. *
  168. * END OF METHODS FOR PART 1 !!!!!!!!
  169. *
  170. *
  171. */
  172.  
  173. /*
  174. * Creates a ListNOde containing value
  175. *
  176. * insert the newly created ListNOde at Front of List
  177. */
  178. public void addFirst(Object value)
  179. {
  180. assertValidType(value);
  181. first = new ListNode(value, first);
  182. }
  183.  
  184. /*
  185. * return the Object stored in the first ListNOde
  186. */
  187. public Object getFirst()
  188. {
  189. if (first == null)
  190. {
  191. throw new NoSuchElementException(); //Don't worry about designing a test to turn this line Green
  192. }
  193. else
  194. return first.getValue();
  195. }
  196.  
  197. /*
  198. * Creates a ListNOde containing value
  199. *
  200. * insert the newly created ListNOde at End of List
  201. */
  202. public void addLast(Object value)
  203. {
  204. assertValidType(value);
  205. ListNode temperature = new ListNode(value, null);
  206. ListNode yuh = first;
  207. ListNode back = null;
  208. while (yuh != null)
  209. {
  210. back = yuh;
  211. yuh = yuh.getNext();
  212. }
  213. if (back == null)
  214. first = temperature;
  215. else
  216. back.setNext(temperature);
  217. }
  218.  
  219. /*
  220. * return the Object stored in the last ListNOde
  221. */
  222. public Object getLast()
  223. {
  224. if (first == null) return null;
  225. ListNode yuh = first;
  226. ListNode back = null;
  227. while (yuh != null)
  228. {
  229. back = yuh;
  230. yuh = yuh.getNext();
  231. }
  232. if (back == null)
  233. return first.getValue();
  234. else
  235. return back.getValue();
  236. }
  237.  
  238. // return the object previously at index ind.
  239. // replace that value with obj!
  240. // preCondition: 0 < ind < size()
  241. // size() > 0
  242. public Object set(int ind, Object obj)
  243. {
  244. int i = 0;
  245. ListNode yuh = first;
  246. Object temperature = yuh.getValue();
  247. while (i < ind)
  248. {
  249. i++;
  250. yuh = yuh.getNext();
  251. temperature = yuh.getValue();
  252. }
  253. yuh.setValue(obj);
  254. return temperature;
  255. }
  256.  
  257. /*
  258. * return a reference to the middle ListNode
  259. *
  260. * That is, the Listnode at index = size() / 2 (remember to use integer Math - truncate/round down
  261. *
  262. * if size() == 0 return null
  263. */
  264. public ListNode getMiddleNode()
  265. {
  266. if ( size() == 0 ) return null;
  267. ListNode temperature = first;
  268. int limit = size()/2;
  269. // if (size() % 2 == 0) limit++;
  270. for (int i = 0; i < limit; i++)
  271. temperature = temperature.getNext();
  272. return temperature;
  273. }
  274.  
  275.  
  276. /*
  277. *
  278. * END OF METHODS FOR PART 2 !!!!!!!!
  279. *
  280. *
  281. */
  282.  
  283. /*
  284. * return the object at index ind.
  285. *
  286. * deletes the ListNode at index = ind
  287. *
  288. * preCondition: 0 <= ind < size()
  289. * 0 < size()
  290. *
  291. * postCondition size has been decreased by 1
  292. */
  293. public Object remove(int ind)
  294. {
  295. int i = 0;
  296. ListNode yuh = first;
  297. ListNode back = null;
  298. while (i < ind)
  299. {
  300. i++;
  301. back = yuh;
  302. yuh = yuh.getNext();
  303. }
  304. if (back == null)
  305. first = first.getNext();
  306. else
  307. back.setNext(yuh.getNext());
  308. return yuh.getValue();
  309. }
  310.  
  311. /*
  312. * contents of the List have been reversed
  313. *
  314. * Sample: if toString().equals("[a, b, c, d, ... ,m]") == true before reverse
  315. *
  316. * then toString().equals("[m, l, k, j, ... ,a]") == true after reverse
  317. *
  318. * if size() == 0, or size() == 1. the List is not modified
  319. *
  320. */
  321. public void reverse()
  322. {
  323. ArrayList l = new ArrayList();
  324. ListNode yuh = first;
  325. while (yuh != null)
  326. {
  327. l.add(0, yuh.getValue());
  328. yuh = yuh.getNext();
  329. }
  330.  
  331. yuh = first;
  332. while (yuh != null)
  333. {
  334. yuh.setValue(l.get(0));
  335. l.remove(0);
  336. yuh = yuh.getNext();
  337. }
  338. }
  339.  
  340. /*
  341. * deletes the first (at smallest index) ListNode in the List such that .getValue().equals(elem) and return true
  342. *
  343. * if no ListNode contains elem, return false and do NOT modify the List
  344. */
  345. public boolean remove(Object elem)
  346. {
  347. ListNode curr = first;
  348. ListNode back = null;
  349.  
  350. while (curr != null && !curr.getValue().equals(elem))
  351. {
  352. back = curr;
  353. curr = curr.getNext();
  354. }
  355. if (curr == null) // elem not found
  356. return false;
  357.  
  358. if (back == null) // first node
  359. {
  360. first = first.getNext();
  361. return true;
  362. }
  363. back.setNext(curr.getNext());
  364. return true;
  365.  
  366. }
  367.  
  368. /*
  369. * deletes all ListNode in the List such that .getValue().equals(elem) and return true
  370. *
  371. * if no ListNode contains elem, return false and do NOT modify the List
  372. */
  373. public boolean removeAll(Object elem)
  374. {
  375. boolean flag = false;
  376. while ( contains(elem))
  377. {
  378. remove(elem);
  379. flag = true;
  380. }
  381. return flag;
  382. }
  383.  
  384. /*
  385. * returns Object stored in first ListNode (use getValue()) in the List
  386. * AND removes that node from the List
  387. *
  388. * preCondition: 0 < size()
  389. *
  390. * postondtion: size() has been decresed by 1
  391. *
  392. */
  393. public Object removeFirst()
  394. {
  395. if (first == null)
  396. return null;
  397. Object temperature = first.getValue();
  398. first = first.getNext();
  399. return temperature;
  400. }
  401.  
  402. // returns Object stored in last node - use getValue()
  403. public Object removeLast()
  404. {
  405. reverse();
  406. Object temperature = removeFirst();
  407. reverse();
  408. return temperature;
  409. }
  410.  
  411. public void inOrderInsert(Comparable c) {
  412. assertValidType(c);
  413.  
  414. ListNode back = null;
  415. ListNode yuh = first;
  416. while (yuh != null && c.compareTo(yuh.getValue()) > 0)
  417. {
  418. back = yuh;
  419. yuh = yuh.getNext();
  420. }
  421. if (back == null) // front insert
  422. {
  423. addFirst(c);
  424. return;
  425. }
  426. ListNode temperature = new ListNode(c, yuh);
  427. back.setNext(temperature);
  428. }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement