Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. PROMPT
  2. 1. addBefore takes an element as input and adds the element to the list being iterated so that the element occurs before the element that was just returned by the most recent call to next. If the list is empty or if next has not been called, this method should throw a NoSuchElementException.
  3. 2. addAfter takes an element as input and adds the element to the list being iterated so that the element occurs immediately after the element that was just returned by the most recent call to next. If the list is empty or if next has not been called, this method should place the element at the beginning of the list.
  4.  
  5. CODE
  6. import java.util.NoSuchElementException;
  7. import java.util.*;
  8.  
  9. /**
  10. * A class to represent a linked list of nodes.
  11. */
  12. public class LinkedList<T> implements Iterable<T> {
  13. /** the first node of the list, or null if the list is empty */
  14. private LLNode<T> front;
  15.  
  16. /**
  17. * Creates an initially empty linked list
  18. */
  19. public LinkedList() {
  20. front = null;
  21. }
  22.  
  23. /**
  24. * Returns the first node.
  25. */
  26. protected LLNode<T> getFront() {
  27. return front;
  28. }
  29.  
  30. /**
  31. * Changes the first node.
  32. * @param node the first node of the new linked list
  33. */
  34. protected void setFront(LLNode<T> node) {
  35. this.front = node;
  36. }
  37.  
  38. public ArrayList<T> toArrayList(){
  39. LLNode<T> nodeptr = getFront();
  40. ArrayList<T> list = new ArrayList<T>();
  41. for (int i = 0; i < length(); i++){
  42. list.add(i, nodeptr.getElement());
  43. nodeptr = nodeptr.getNext();
  44. }
  45. return list;
  46. }
  47.  
  48. /**
  49. * Add an element to the front of the linked list
  50. */
  51. public void addToFront(T element) {
  52. setFront(new LLNode<T>(element, getFront()));
  53. }
  54.  
  55. /**
  56. * Return whether the list is empty
  57. * @return true if the list is empty
  58. */
  59. public boolean isEmpty() {
  60. return (getFront() == null);
  61. }
  62.  
  63. /**
  64. * Returns the length of the linked list
  65. * @return the number of nodes in the list
  66. */
  67. public int length() {
  68. int count = 0; // counts number of nodes seen
  69. LLNode<T> nodeptr = getFront();
  70. while (nodeptr != null) {
  71. count++;
  72. nodeptr = nodeptr.getNext();
  73. }
  74. return count;
  75. }
  76.  
  77. /**
  78. * Remove and return the element at the front of the list
  79. * @return the first element of the list
  80. * @throws NoSuchElementException if there is no such element
  81. */
  82. public T removeFromFront() {
  83. if (isEmpty())
  84. throw new NoSuchElementException();
  85. else {
  86. T save = getFront().getElement();
  87. setFront(getFront().getNext());
  88. return save;
  89. }
  90. }
  91.  
  92. /**
  93. * Add an element to the very end of the list
  94. * @param element the element to add to the end of the list
  95. */
  96. public void addToEnd(T element) {
  97. if (isEmpty())
  98. addToFront(element);
  99. else {
  100. LLNode<T> nodeptr = getFront();
  101. // the loop will end with nodeptr looking at the last node in list
  102. while (nodeptr.getNext() != null)
  103. nodeptr = nodeptr.getNext();
  104. nodeptr.setNext(new LLNode<T>(element, null));
  105. }
  106. }
  107.  
  108. public class ThisListIterator implements LLIterator<T>{
  109.  
  110. private boolean hasstarted;
  111.  
  112. /** points to the current node of the iteration */
  113. private LLNode<T> nodeptr;
  114.  
  115. /** Create an iterator for the list starting at the inputted node
  116. * @param front the first node for the iteration
  117. */
  118. public ThisListIterator(LLNode<T> front) {
  119. nodeptr = front;
  120. hasstarted = false;
  121. }
  122.  
  123. /**
  124. * Returns true if there are more nodes to run through
  125. * @return true if there are still more values in the iteration
  126. */
  127. public boolean hasNext() {
  128. return nodeptr != null;
  129. }
  130.  
  131. @Override
  132. public void addBefore(T element){
  133. if (isEmpty() || hasstarted == false){
  134. throw new NoSuchElementException();
  135. }
  136. else{
  137. nodeptr.setNext(new LLNode<T>(element, nodeptr));
  138. }
  139. }
  140.  
  141. @Override
  142. public void addAfter(T element){
  143. if (isEmpty() || hasstarted == false){
  144. addToFront(element);
  145. }
  146. else{
  147. nodeptr.insertAfter(element);
  148. }
  149. }
  150.  
  151. /**
  152. * Returns the next value stored in the linked list
  153. * @return the next value of the linked list
  154. */
  155. public T next() {
  156. boolean hasstarted = true;
  157. T save = nodeptr.getElement();
  158. nodeptr = nodeptr.getNext();
  159. return save;
  160. }
  161.  
  162. /** Not implemented */
  163. public void remove() {
  164. throw new UnsupportedOperationException();
  165. }
  166. }
  167.  
  168. /**
  169. * Required by the Iterable interface.
  170. * @return an LLiterator for the list
  171. */
  172. public LLIterator<T> iterator() {
  173. return new ThisListIterator(getFront());
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement