Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. public class ReferenceBasedList implements ListInterface
  2. {
  3. private ListNode head;
  4. private ListNode tail;
  5. int numItems;
  6.  
  7. public ReferenceBasedList()
  8. {
  9. head = tail = null;
  10. numItems = 0;
  11. }
  12.  
  13. public int size()
  14. {
  15. return numItems;
  16. }
  17.  
  18. public boolean isEmpty()
  19. {
  20. return (numItems == 0);
  21. }
  22.  
  23. public void removeAll()
  24. {
  25. head = tail = null;
  26. numItems = 0;
  27. }
  28.  
  29. private ListNode find(int index)
  30. {
  31. ListNode curr = head;
  32. for (int skip = 1; skip < index; skip++)
  33. curr = curr.getNext();
  34. return curr;
  35. }
  36.  
  37. public Object get(int index)
  38. throws ListIndexOutOfBoundsException
  39. {
  40. if (index >= 1 && index <= numItems)
  41. {
  42. ListNode curr = find(index);
  43. return curr.getItem();
  44. }
  45. else
  46. {
  47. throw new ListIndexOutOfBoundsException(
  48. "List index out of bounds exception on get");
  49. }
  50. }
  51.  
  52. public void add(int index, Object newDataItem)
  53. throws ListIndexOutOfBoundsException
  54. {
  55. if (index >= 1 && index <= numItems+1)
  56. {
  57. if ( index == 1 )
  58. {
  59. ListNode newNode = new ListNode(newDataItem, head);
  60. head = newNode;
  61.  
  62. if (tail==null)
  63. tail = head;
  64. }
  65. else if ( index==numItems+1 )
  66. {
  67. ListNode newNode = new ListNode(newDataItem);
  68. tail.setNext(newNode);
  69. tail = newNode;
  70. }
  71. else
  72. {
  73. ListNode prev = find(index-1);
  74. ListNode newNode = new ListNode(newDataItem, prev.getNext());
  75. prev.setNext(newNode);
  76. }
  77. numItems++;
  78. }
  79. else
  80. {
  81. throw new ListIndexOutOfBoundsException(
  82. "List index out of bounds exception on add");
  83. }
  84. }
  85.  
  86. public void insert(Object newDataItem)
  87. {
  88. this.add(1,newDataItem);
  89. }
  90.  
  91. public void append(Object newDataItem)
  92. {
  93. this.add(numItems+1,newDataItem);
  94. }
  95.  
  96. public Object showFront()
  97. {
  98. return this.get(1);
  99. }
  100.  
  101. public Object showLast()
  102. {
  103. return this.get(numItems);
  104. }
  105.  
  106. public void remove(int index)
  107. throws ListIndexOutOfBoundsException
  108. {
  109. if (index >= 1 && index <= numItems)
  110. {
  111. if (index == 1)
  112. {
  113. head = head.getNext();
  114. if (head == null)
  115. tail = null;
  116. }
  117. else
  118. {
  119. ListNode prev = find(index-1);
  120. ListNode curr = prev.getNext();
  121. prev.setNext(curr.getNext());
  122. if (index == numItems)
  123. tail = prev;
  124. }
  125. numItems--;
  126. }
  127. else
  128. {
  129. throw new ListIndexOutOfBoundsException(
  130. "List index out of bounds exception on remove");
  131. }
  132. }
  133.  
  134. public boolean exists(Object dataItem)
  135. {
  136. for (ListNode tmp=head; tmp!=null; tmp=tmp.getNext())
  137. if (tmp.getItem().equals(dataItem))
  138. return true;
  139. return false;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement