Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. public class MyList<E> implements SingleList<E> {
  2.  
  3. private Node<E> head;
  4. private Node<E> tail;
  5. private int size;
  6.  
  7. public MyList() {
  8. this.head = null;
  9. this.tail = null;
  10. this.size = 0;
  11. }
  12.  
  13. private static class Node<E> {
  14. E data;
  15. Node<E> next;
  16.  
  17. public Node(E data, Node<E> next) {
  18. this.data = data;
  19. this.next = next;
  20. }
  21.  
  22. public void setNext(Node<E> next) {
  23. this.next = next;
  24. }
  25. }
  26.  
  27. //ITERATOR
  28. private class iter implements Iterator<E> {
  29. Node<E> previousNode;
  30. Node<E> currentNode;
  31. Node<E> nextNode;
  32.  
  33. public iter() {
  34. previousNode = null;
  35. currentNode = null;
  36. nextNode = head;
  37. }
  38.  
  39. @Override
  40. public boolean hasNext() {
  41. return nextNode != null;
  42. }
  43.  
  44. @Override
  45. public E next() {
  46. if (!hasNext()) {
  47. throw new NoSuchElementException("No more elements");
  48. }
  49. previousNode = currentNode;
  50. currentNode = nextNode;
  51. nextNode = nextNode.next;
  52. return currentNode.data;
  53. }
  54.  
  55. @Override
  56. public void remove() {
  57. if (currentNode == null) {
  58. throw new IllegalStateException();
  59. }
  60. if (previousNode == null) {
  61. head = nextNode;
  62. } else {
  63. previousNode.next = nextNode;
  64. currentNode = previousNode;
  65. }
  66. size--;
  67. }
  68. }
  69.  
  70. //KLAR
  71. @Override
  72. public Iterator<E> iterator() {
  73. return new iter();
  74. }
  75.  
  76. @Override
  77. public void add(E element) {
  78. Node<E> newNode = new Node<E>(element, null);
  79. if (head == null) {
  80. head = newNode;
  81. tail = newNode;
  82. } else {
  83. tail.next = newNode;
  84. tail = newNode;
  85. }
  86. size++;
  87. }
  88.  
  89. //KLAR
  90. @Override
  91. public void add(int index, E element) {
  92. if (index < 0 || index > size) {
  93. throw new IndexOutOfBoundsException(Integer.toString(index));
  94. }
  95. if (index == size) {
  96. add(element);
  97. return;
  98. }
  99. if (index == 0) {
  100. head = new Node<E>(element, head);
  101. } else {
  102.  
  103. Node<E> current = head;
  104. for (int i = 0; i < index - 1 && current != null; i++) {
  105. current = current.next;
  106. }
  107. current.setNext(new Node<E>(element, current.next));
  108. }
  109. size++;
  110. }
  111.  
  112. //REMOVE by index METHOD
  113. @Override
  114. public E remove(int index) {
  115. if (index < 0 || index >= size()) {
  116. throw new IndexOutOfBoundsException("Out of bounds");
  117. }
  118.  
  119. Iterator<E> iterator = this.iterator();
  120.  
  121. for (int j = 0; j < index; j++) {
  122. iterator.next();
  123. }
  124. E element = iterator.next();
  125. iterator.remove();
  126. return element;
  127. }
  128.  
  129. //REMOVE element
  130. @Override
  131. public boolean remove(E element) {
  132. Iterator<E> iterator = this.iterator();
  133.  
  134. while(iterator.hasNext()){
  135. E e = iterator.next();
  136.  
  137. if(element==null ? e==null : element.equals(e)){
  138. iterator.remove();
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144.  
  145. @Before
  146. public void setUp() {
  147. list.add("First");
  148. list.add("Second");
  149. list.add("Third");
  150. list.add("Fourth");
  151. list.add("Fifth");
  152. }
  153.  
  154. @Test
  155. public void testSimpleMethodsOnDefaultList() {
  156. assertEquals(5, list.size());
  157. assertEquals("First", list.get(0));
  158. assertEquals("Third", list.get(2));
  159. assertEquals("Fifth", list.get(4));
  160. assertEquals("[First, Second, Third, Fourth, Fifth]", list.toString());
  161.  
  162. list.add("Second");
  163. assertEquals(6, list.size());
  164. assertEquals("Second", list.get(5));
  165. }
  166.  
  167. @Test
  168. public void testRemoveWithIndex() {
  169. printList();
  170. assertEquals("Third", list.remove(2));
  171. assertEquals(4, list.size());
  172. assertEquals("Second", list.get(1));
  173. assertEquals("Fourth", list.get(2));
  174.  
  175. assertEquals("First", list.remove(0));
  176. assertEquals(3, list.size());
  177. assertEquals("Second", list.get(0));
  178. System.out.println();
  179. for(String s : list){
  180. System.out.print(s + ", ");
  181. }
  182. System.out.println();
  183.  
  184. System.out.println(list.get(0) + ", " + list.get(1) + ", " + list.get(2));
  185. assertEquals("Fifth", list.remove(2));
  186. assertEquals(2, list.size());
  187. System.out.println(list.get(0) + ", " + list.get(1) + ", " + list.size());
  188. assertEquals("Fourth", list.get(1));
  189. }
  190.  
  191. Node<E> currentLink = head;
  192. Node<E> previousLink = head;
  193. for(int i = 0; i < index; i++){
  194. if(currentLink.next == null){
  195.  
  196. return null;
  197. } else {
  198. previousLink = currentLink;
  199. currentLink = currentLink.next;
  200. }
  201. }
  202. if(currentLink == head){
  203. head = head.next;
  204. } else {
  205. previousLink.next = currentLink.next;
  206. }
  207.  
  208. size--;
  209. return currentLink.data;
  210.  
  211. public String toString() {
  212. if (head != null) {
  213. Iterator<E> iterator = this.iterator();
  214. String storeData = "[";
  215. while (iterator.hasNext()) {
  216. E e = iterator.next();
  217. if (iterator.hasNext() == false) {
  218. return storeData += e + "]";
  219. }
  220. storeData += e + ", ";
  221. }
  222. return storeData + "]";
  223. }
  224. return "[]";
  225. }
Add Comment
Please, Sign In to add comment