Advertisement
Guest User

ListReferenceBased

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