Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1.  
  2. // *********************************************
  3. // Reference-based implementation of ADT list.
  4. // *********************************************
  5. public class ListReferenceBased implements ListInterface
  6. {
  7. // reference to linked list of items
  8. private Node head;
  9.  
  10. // definitions of constructors and methods
  11. public ListReferenceBased()
  12. {
  13. head = null;
  14. } // end default constructor
  15.  
  16. public boolean isEmpty()
  17. {
  18. } // end isEmpty
  19.  
  20. public int size()
  21. {
  22. } // end size
  23.  
  24. public Node find(int index)
  25. {
  26. // --------------------------------------
  27. // Locates a specified node in a linked list.
  28. // Precondition: index is the number of the desired
  29. // Postcondition: Returns a reference to the desired node.
  30. // --------------------------------------------------------
  31. Node curr = head;
  32. for (int skip = 0; skip < index; skip++)
  33. {
  34. curr = curr.next;
  35. } // end for
  36. return curr;
  37. } // end find
  38.  
  39. public Comparable get(int index) throws ListIndexOutOfBoundsException
  40. {
  41. if (index >= 0)
  42. {
  43. // get reference to node, then data in node
  44. Node curr = find(index);
  45. Comparable dataItem = curr.item;
  46. return dataItem;
  47. }
  48. else
  49. {
  50. throw new ListIndexOutOfBoundsException("List index out of bounds on get");
  51. } // end if
  52. } // end get
  53.  
  54. public void add(int index, Comparable item) throws ListIndexOutOfBoundsException
  55. {
  56. if (index >= 0)
  57. {
  58. if (index == 0)
  59. {
  60. // insert the new node containing item at beginning of list
  61. Node newNode = new node(item, head);
  62. head = newNode;
  63. }
  64. else
  65. {
  66. Node prev = find(index-1);
  67.  
  68. // insert the new node containing item after
  69. // the node that prev references
  70. Node newNode = new Node(item, prev.next);
  71. prev.next = newNode;
  72. } // end if
  73. } // end if
  74. else
  75. {
  76.  
  77. throw new ListIndexOutOfBoundsException("List index out of bounds on add");
  78. } // end if
  79. } // end add
  80.  
  81. public void remove(int index) throws ListIndexOutOfBoundsException
  82. {
  83. if (index >= 0)
  84. {
  85. if(index == 0)
  86. {
  87. // delete the first node from the list
  88. head = head.next;
  89. }
  90. else
  91. {
  92. Node prev = find(index-1);
  93. // delete the node after the node that prev
  94. // references, save reference to node
  95. Node curr = prev.next;
  96. prev.next = curr.next;
  97. } // end if
  98. } // end if
  99. else
  100. {
  101. throw new ListIndexOutOfBoundsException("List index out of bounds on remove");
  102. } // end if
  103. } // end remove
  104.  
  105. public void removeAll()
  106. {
  107. //setting head to null causes list to be unreachable and thus
  108. //marked for garbage collection
  109. head = null;
  110. } // end removeAll
  111.  
  112. //returns true if ListReferenceBased object is sorted else return false
  113. public boolean isSorted()
  114. {
  115.  
  116. }
  117.  
  118. //adjust the node references so that the items
  119. //are in reverse of the original order, without creating any new nodes
  120. public void reverseList()
  121. {
  122.  
  123. }
  124. }
  125.  
  126. ___________________________________________________________________________________________________________________
  127.  
  128. class Node
  129. {
  130. Comparable item;
  131. Node next;
  132.  
  133. Node(Object newItem)
  134. {
  135. item = newItem;
  136. next = null;
  137. } // end constructor
  138.  
  139. Node(Object newItem, Node nextNode)
  140. {
  141. item = newItem;
  142. next = nextNode;
  143. } // end constructor
  144. } // end class Node
  145.  
  146. ___________________________________________________________________________________________________________________
  147.  
  148. // *************************************
  149. // Interface for the ADT List
  150. // *************************************
  151. public interface ListInterface
  152. {
  153. //list operations:
  154. public boolean isEmpty();
  155. public int size();
  156. public void add(int index, Object item)
  157. throws ListIndexOutOfBoundsException;
  158. public void remove(int index)
  159. throws ListIndexOutOfBoundsException;
  160. public Object get(int index)
  161. throws ListIndexOutOfBoundsException;
  162. public void removeAll();
  163. } // end ListInterface
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement