Advertisement
Guest User

ListReferenceBased

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