Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package SingleLP;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5.  
  6. public class SingleLinkedListUpg3<E> implements Iterable<E> {
  7.  
  8. /**
  9. * Node Class, represent an element in the singleLinkedList
  10. * contains the element and the Node to the next element in the list
  11. */
  12. private static class Node<E>{
  13.  
  14. private E data;
  15. private Node<E> next;
  16.  
  17. public Node(E data, Node<E> next) {
  18. this.data = data;
  19. this.next = next;
  20. }
  21. }
  22.  
  23. /**
  24. * Itr class, iterator for traversing the singleLinkedList
  25. */
  26. private class Itr implements Iterator<E> {
  27. Node<E> c, bc, bbc;
  28. boolean removable;
  29.  
  30. public Itr(Node<E> start){
  31. c = start;
  32. bc = null;
  33. bbc = null;
  34. removable = false;
  35. }
  36.  
  37. @Override
  38. public boolean hasNext() {
  39. return c!=null;
  40. }
  41.  
  42. @Override
  43. public E next() {
  44. if(c == null) throw new NoSuchElementException();
  45.  
  46. if(!removable) {
  47. bbc = bc;
  48. bc = c;
  49. } else {
  50. bc = c;
  51. removable = false;
  52. }
  53. E data = c.data;
  54. c = c.next;
  55.  
  56. return data;
  57. }
  58.  
  59. @Override
  60. public void remove() {
  61. if(removable) throw new IllegalStateException();
  62.  
  63. if(bbc == null){
  64. bc = null;
  65. head = head.next;
  66. } else if (size==1){
  67. head = null;
  68. } else {
  69. bbc.next = c;
  70. }
  71. size--;
  72. removable = true;
  73. }
  74. }
  75.  
  76. /**
  77. * Start of SingleLinkedList Methods, fields, constructors etc.
  78. */
  79. private Node<E> head;
  80. private int size;
  81.  
  82. public SingleLinkedListUpg3(){
  83. head = null;
  84. size = 0;
  85. }
  86.  
  87. public void add(int index, E item){
  88. if(index<0 || index > size)
  89. throw new IndexOutOfBoundsException(Integer.toString(index));
  90. if(index==0)
  91. addFirst(item);
  92. else{
  93. Node<E> node = getNode(index - 1);
  94. addAfter(node, item);
  95. }
  96. }
  97.  
  98.  
  99. private void addFirst(E item){
  100. head = new Node<E>(item,head);
  101. size++;
  102. }
  103.  
  104. private Node<E> getNode(int index){
  105. Node<E> node = head;
  106. for (int i = 0; i < index && node!=null ; i++) {
  107. node = node.next;
  108. }
  109. return node;
  110. }
  111.  
  112. private void addAfter(Node<E> node, E item){
  113. node.next = new Node<>(item, node.next);
  114. size++;
  115. }
  116.  
  117. public E get(int index){
  118. if(index<0 || index >= size)
  119. throw new IndexOutOfBoundsException(Integer.toString(index));
  120. Node<E> node = getNode(index);
  121. return node.data;
  122. }
  123.  
  124. public E remove(int index){
  125. if(index<0 || index >= size)
  126. throw new IndexOutOfBoundsException(Integer.toString(index));
  127.  
  128. E removedData;
  129. if(size == 1){
  130. removedData = head.data;
  131. head = null;
  132. } else {
  133. if(index == 0){
  134. removedData = head.data;
  135. head = head.next;
  136. } else{
  137. Node<E> node = getNode(index-1);
  138. removedData = node.next.data;
  139. node.next = node.next.next;
  140. }
  141. }
  142. size--;
  143. return removedData;
  144. }
  145.  
  146. public int size(){
  147. return size;
  148. }
  149.  
  150. public boolean add(E item){
  151. add(size, item);
  152. return true;
  153. }
  154.  
  155. public Iterator<E> iterator(){
  156. return new Itr(head);
  157. }
  158.  
  159. @Override
  160. public String toString() {
  161. StringBuilder sb = new StringBuilder("[");
  162. if(size!=0){
  163. Node<E> p = head;
  164. if(p!=null){
  165. while (p.next!=null){
  166. sb.append(p.data.toString());
  167. sb.append(" ==> ");
  168. p = p.next;
  169. }
  170. sb.append(p.data.toString());
  171. }
  172. sb.append("]");
  173. sb.append("\nSize: ").append(size).append(" Head: ").append(head.data);
  174. } else sb.append("]");
  175. return sb.toString();
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement