Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. /*
  2. * This application is a singly linked-list that is
  3. * the code base for a programming assignment.
  4. *
  5. * @creator gdt
  6. * @created 02017.04.01
  7. * @updated 02017.10.27
  8. */
  9.  
  10. class Node {
  11.  
  12. // instance variables...
  13. private Object item;
  14. private Node next;
  15.  
  16. // constructor...
  17. public Node(Object x) { item = x; next = null; }
  18.  
  19. // getter/setter methods...
  20. public Object getItem() { return item; }
  21. public Node getNext() { return next; }
  22. public void setItem(Object item) { this.item = item; }
  23. public void setNext(Node n) { next = n; }
  24.  
  25. // return a String object representation of "this" Node object...
  26. public String toString() {
  27. return item + " (next is " + (next == null ? "" : "not ") + "null)";
  28. }
  29.  
  30. }
  31.  
  32. /*
  33. * class MyLinkedList is not a complete class (i.e. there are
  34. * missing methods). The class was primarily created to support
  35. * the Queue data structure.
  36. */
  37.  
  38. public class MyLinkedList {
  39.  
  40. private Node head = null; // nth(1) is the head Node object
  41. private int size = 0; // size == 0 implies empty linked-list
  42.  
  43. // return the number of items in "this" linked-list...
  44. public int getSize() { return size; }
  45.  
  46. // return the head Node for "this" linked-list...
  47. public Node getHead() { return head; }
  48.  
  49. // set the size of "this" linked-list to zero...
  50. public void clear() { size = 0; head = null; }
  51.  
  52. /*
  53. * TBI (To Be Implemented) -- The remaining instance methods
  54. * need to be implemented as part of the #MyLinkedList assignment.
  55. */
  56.  
  57. /**
  58. * TBI (To Be Implemented)
  59. * Prints "this" linked-list starting at the Node object parameter.
  60. * The print format: [ followed by a space followed the elements
  61. * separated by a space followed by ] Example: [ 0 1 2 3 4 1 ]
  62. *
  63. * @param Node to begin printing from
  64. * @return this MyLinkedList
  65. */
  66. public MyLinkedList print(Node n) {
  67. StringBuilder output = new StringBuilder();
  68.  
  69. output.append("[ ");
  70. while (n.getNext() != null) {
  71. output.append(n.getItem() + " ");
  72. n = n.getNext();
  73. }
  74. output.append("]");
  75. System.out.println(output.toString());
  76.  
  77. return this;
  78. }
  79.  
  80. /**
  81. * TBI (To Be Implemented)
  82. * Removes an item from this MyLinkedList object.
  83. *
  84. * @param item to remove from this MyLinkedList object
  85. * @return true if item removed; else return false
  86. */
  87. public boolean remove(Object item) {
  88. if (getHead() == null) // check if list is empty
  89. return false;
  90.  
  91. Node previous = getHead();
  92.  
  93. if (previous.getItem() == item) {
  94. head = previous.getNext();
  95. return true;
  96. }
  97.  
  98. Node current = getHead().getNext();
  99.  
  100. if (current == null) { // check if there's one node in the list
  101. previous = null;
  102. size--;
  103. return true;
  104. }
  105.  
  106. Node next = getHead().getNext().getNext(); // now it's safe to set the next node
  107.  
  108. while (current.getItem() != item) { // traverse to reach the item
  109. if (next == null) { // check if next is null
  110. return false;
  111. }
  112.  
  113. previous = previous.getNext();
  114. current = current.getNext();
  115. next = next.getNext();
  116. }
  117.  
  118. previous.setNext(next);
  119. size--;
  120. return true;
  121. }
  122.  
  123. /**
  124. * TBI (To Be Implemented)
  125. * Inserts an item into this MyLinkList object.
  126. *
  127. * Insert examples:
  128. * [7 8 9] insert(5, 0) does nothing (return false)
  129. * [7 8 9] insert(5, 1) results in [5 7 8 9]
  130. * [7 8 9] insert(5, 2) results in [7 5 8 9]
  131. * [7 8 9] insert(5, 3) results in [7 8 5 9]
  132. * [7 8 9] insert(5, 4) does nothing (return false)
  133. *
  134. * @param item to insert into this MyLinkedList object
  135. * @param index where item is to be inserted
  136. * @return true if item inserted; else return false
  137. */
  138. public boolean insert(Object item, int at_i) {
  139. Node n = getHead();
  140. boolean itemInserted = false;
  141.  
  142. for (int i = 0; i < at_i; i++) {
  143. if (i > size);
  144.  
  145.  
  146. }
  147.  
  148. return itemInserted;
  149. }
  150.  
  151. /*
  152. * TBI (To Be Implemented
  153. *
  154. * Fill is this method comment block for add()
  155. * in a way that is consistent with the other
  156. * TBI method comments blocks.
  157. */
  158. public MyLinkedList add(Object item) {
  159. size++;
  160. Node n = new Node(item);
  161. if (head == null)
  162. head = n;
  163. else {
  164. Node t = head;
  165. while (t.getNext() != null)
  166. t = t.getNext();
  167. t.setNext(n);
  168. }
  169. return this;
  170. }
  171.  
  172. /*
  173. * test class MyLinkedList...
  174. */
  175. public static void main(String[] argv) {
  176.  
  177. MyLinkedList list = new MyLinkedList();
  178. Integer[] z = { new Integer(0), new Integer(1), new Integer(2),
  179. new Integer(3), new Integer(4), new Integer(1), };
  180. for (int i = 0; i < z.length; i++)
  181. list.add(z[i]);
  182. System.out.println("test remove()...");
  183. System.out.print("linked-list: ");
  184. list.print(list.head);
  185. remove(list, z[1]);
  186. remove(list, z[0]);
  187. remove(list, z[1]);
  188. remove(list, z[4]);
  189. remove(list, z[2]);
  190. remove(list, z[3]);
  191. remove(list, z[2]);
  192.  
  193. System.out.println("\ntest insert()...");
  194. for (int i = 0; i < z.length; i++)
  195. list.add(z[i]);
  196. System.out.print("linked-list: ");
  197. list.print(list.head);
  198. Integer rm0 = new Integer(9);
  199. Integer rm1 = new Integer(6);
  200. insert(list, rm0, 2);
  201. insert(list, new Integer(8), 0);
  202. insert(list, new Integer(5), 9);
  203. insert(list, rm1, list.getSize());
  204. //insert(list, rm1, 1);
  205. remove(list, rm0);
  206. remove(list, rm1);
  207. }
  208.  
  209. static void remove(MyLinkedList mll, Object i) {
  210. char c = mll.remove(i) ? 'T' : 'F';
  211. System.out.print("remove(" + i + "): " + c + "; ");
  212. mll.print(mll.head);
  213. }
  214.  
  215. static void insert(MyLinkedList mll, Object o, int i) {
  216. char c = mll.insert(o, i) ? 'T' : 'F';
  217. System.out.print("insert(" + o + ", " + i + "): " + c + "; ");
  218. mll.print(mll.head);
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement