Commit d5759da3da03bda652efa8e485f0186b838d7bb7

Authored by Adrian Chirita
1 parent f471bc76

alle metoder er nå implementert.

.classpath 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  4 + <classpathentry kind="src" path="src"/>
  5 + <classpathentry kind="output" path="bin"/>
  6 +</classpath>
... ...
.gitignore
1 1 out/
2   -.idea/
3 2 \ No newline at end of file
  3 +.idea/
  4 +/bin/
... ...
.project 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<projectDescription>
  3 + <name>TwoWayLinkedList</name>
  4 + <comment></comment>
  5 + <projects>
  6 + </projects>
  7 + <buildSpec>
  8 + <buildCommand>
  9 + <name>org.eclipse.jdt.core.javabuilder</name>
  10 + <arguments>
  11 + </arguments>
  12 + </buildCommand>
  13 + </buildSpec>
  14 + <natures>
  15 + <nature>org.eclipse.jdt.core.javanature</nature>
  16 + </natures>
  17 +</projectDescription>
... ...
src/TwoWayLinkedList.java
1 1 import java.util.Iterator;
2 2 import java.util.ListIterator;
3 3  
4   -
5 4 public class TwoWayLinkedList<E> implements MyList<E> {
6 5 private Node<E> head, tail;
7   - private int size = 0;
8   -
9   - /** Create a default list */
10   - public TwoWayLinkedList() {}
11   -
12   - /** Create a list from an array of objects */
13   - public TwoWayLinkedList(E[] objects) {}
14   -
15   - /** Return the head element in the list */
  6 + private int size;
  7 +
  8 + public TwoWayLinkedList() {
  9 + size = 0;
  10 + head = null;
  11 + tail = null;
  12 + }
  13 + public TwoWayLinkedList(E[] objects) {
  14 + for (int i = 0; i < objects.length; i++)
  15 + this.add(objects[i]);
  16 + }
16 17 public E getFirst() {
17 18 return head.element;
18   - }
19   -
20   - /** Return the last element in the list */
  19 + }
21 20 public E getLast() {
22 21 return tail.element;
23 22 }
24   -
25   - /** Add an element to the beginning of the list */
26 23 public void addFirst(E e) {
27 24 Node<E> currentFirst = head;
28 25 head = new Node<>(e);
... ... @@ -33,53 +30,45 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
33 30 currentFirst.previous = head;
34 31 }
35 32 size++;
36   - }
37   -
38   - /** Add an element to the end of the list */
  33 + }
39 34 public void addLast(E e) {
40   - Node<E> temp = tail;
41   - tail = new Node<>(e);
42   - tail.previous = temp;
43   - if (head == null) {
44   - head = tail;
45   - } else {
46   - temp.next = tail;
47   - }
48   - size++;
  35 + Node<E> newNode = new Node<>(e);
  36 + if (tail == null) {
  37 + head = tail = newNode;
  38 + }
  39 + else {
  40 + tail.next = newNode;
  41 + newNode.previous = tail;
  42 + tail = newNode;
  43 + }
  44 + size++;
49 45 }
50   -
51   - /**
52   - * Add a new element at the specified index in this list The index of the
53   - * head element is 0
54   - */
55 46 public void add(int index, E e) {
56   - if (index == 0) {
57   - addFirst(e);
58   - }
59   - else if (index >= size) {
60   - addLast(e);
61   - }
62   - else {
63   - if (index > size/2) {
64   -
65   - } else {
66   - Node<E> current = head;
67   - for (int i = 1; i < index; i++) {
68   - current = current.next;
69   - }
70   - Node<E> temp = current.next;
71   - current.next = new Node<>(e);
72   - (current.next).next = temp;
73   - (current.next).previous = temp.previous;
74   - size++;
75   - }
76   - }
77   - }
78   -
79   - /**
80   - * Remove the head node and return the object that is contained in the
81   - * removed node.
82   - */
  47 + if (index < 0 || index >= size) {
  48 + throw new IndexOutOfBoundsException();
  49 + }
  50 + else if (index == 0) {
  51 + addFirst(e);
  52 + }
  53 + else if (index == size-1) {
  54 + addLast(e);
  55 + }
  56 + else {
  57 + Node<E> toBeAdded = new Node<>(e);
  58 + Node<E> previous = head;
  59 +
  60 + for(int i = 0; i < index; i++) {
  61 + previous = previous.next;
  62 + }
  63 + Node<E> current = previous.next;
  64 +
  65 + previous.next = toBeAdded;
  66 + toBeAdded.previous = previous;
  67 + toBeAdded.next = current;
  68 + current.previous = toBeAdded;
  69 + size++;
  70 + }
  71 + }
83 72 public E removeFirst() {
84 73 if (size == 0) {
85 74 return null;
... ... @@ -96,11 +85,6 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
96 85 return temp;
97 86 }
98 87 }
99   -
100   - /**
101   - * Remove the last node and return the object that is contained in the
102   - * removed node.
103   - */
104 88 public E removeLast() {
105 89 if (size == 0) {
106 90 return null;
... ... @@ -119,36 +103,31 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
119 103 return temp;
120 104 }
121 105 }
122   -
123   - /**
124   - * Remove the element at the specified position in this list. Return the
125   - * element that was removed from the list.
126   - */
127 106 public E remove(int index) {
128   - if (index < 0 || index >= size) {
129   - return null;
130   - }
131   - else if (index == 0) {
132   - return removeFirst();
133   - }
134   - else if (index == size - 1) {
135   - return removeLast();
136   - }
137   - else {
138   - Node<E> previous = head;
139   -
140   - for (int i = 1; i < index; i++) {
141   - previous = previous.next;
142   - }
143   -
144   - Node<E> current = previous.next;
145   - previous.next = current.next;
146   - current.previous.next = previous.next;
147   - size--;
148   - return current.element;
149   - }
  107 + if(index < 0 || index >= size) {
  108 + throw new IndexOutOfBoundsException();
  109 + }
  110 + else if(index == 0) {
  111 + return removeFirst();
  112 + }
  113 + else if(index == size-1) {
  114 + return removeLast();
  115 + }
  116 + else {
  117 + Node<E> previous = head;
  118 +
  119 + for(int i = 0; i < index; i++) {
  120 + previous = previous.next;
  121 + }
  122 +
  123 + Node<E> current = previous.next;
  124 + previous.next = current.next;
  125 + current.next.previous = previous;
  126 +
  127 + size--;
  128 + return current.element;
  129 + }
150 130 }
151   -
152 131 @Override
153 132 public String toString() {
154 133 StringBuilder result = new StringBuilder("[");
... ... @@ -164,100 +143,226 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
164 143 result.append("]"); // Insert the closing ] in the string
165 144 }
166 145 }
167   -
168 146 return result.toString();
169 147 }
170   -
171   - /** Clear the list */
172 148 public void clear() {
173 149 size = 0;
174 150 head = tail = null;
  151 + }
  152 + public boolean contains(Object e) {
  153 + return indexOf(e) >= 0 ? true:false ;
  154 + }
  155 + public E get(int index) {
  156 + if(index < 0 || index >= size) {
  157 + throw new IndexOutOfBoundsException();
  158 + }
  159 + else if(tail == null) {
  160 + return null;
  161 + }
  162 + else if(index == 0) {
  163 + return head.element;
  164 + }
  165 + else if(index == size-1) {
  166 + return tail.element;
  167 + }
  168 + else {
  169 + Node<E> current = head;
  170 + for (int i = 0; i < index; i++) {
  171 + current = current.next;
  172 + }
  173 + return current.next.element;
  174 + }
175 175 }
176   -
177   - /** Return true if this list contains the element o */
178   - public boolean contains(Object e) {return false;}
179   -
180   - /** Return the element from this list at the specified index */
181   - public E get(int index) {return null;}
182   -
183   - /**
184   - * Return the index of the head matching element in this list. Return -1 if
185   - * no match.
186   - */
187 176 public int indexOf(Object e) {
188   - return -1;
  177 + if (tail == null) {
  178 + return -1;
  179 + }
  180 + else {
  181 + Node<E> current = head;
  182 + for(int i = 0; i < size; i++) {
  183 + if (current.element.equals(e)) return i;
  184 + current = current.next;
  185 + }
  186 + }
  187 + return -1;
189 188 }
190   -
191   - /**
192   - * Return the index of the last matching element in this list Return -1 if
193   - * no match.
194   - */
195 189 public int lastIndexOf(Object e) {
196   - return -1;
  190 + if(tail == null) {
  191 + return -1;
  192 + }
  193 + else {
  194 + Node<E> current = tail;
  195 + for(int i = size-1; i >= 0; i--) {
  196 + if(current.element.equals(e)) return i;
  197 + current = current.previous;
  198 + }
  199 + }
  200 + return -1;
197 201 }
198   -
199   - /**
200   - * Replace the element at the specified position in this list with the
201   - * specified element.
202   - */
203   - public E set(int index, E e) {
204   - return null;
  202 + public E set(int index, E e) {
  203 + if(index < 0 || index >= size) {
  204 + throw new IndexOutOfBoundsException();
  205 + }
  206 + else if (index == 0) {
  207 + E replacedElement = head.element;
  208 + head.element = e;
  209 + return replacedElement;
  210 + }
  211 + else if(index == size-1) {
  212 + E replacedElement = tail.element;
  213 + tail.element = e;
  214 + return replacedElement;
  215 + }
  216 + else {
  217 + Node<E> previous = head;
  218 + for (int i = 0; i < index; i++) {
  219 + previous = previous.next;
  220 + }
  221 + E replacedElement = previous.next.element;
  222 + previous.next.element = e;
  223 + return replacedElement;
  224 + }
205 225 }
206   -
207   - private class LinkedListIterator implements ListIterator<E> {
208   - // datamedlemmer
209   - public LinkedListIterator() {}
210   -
211   -
212   - public LinkedListIterator(int index) {}
213   -
214   - public void setLast() {}
215   -
216   - @Override
217   - public boolean hasNext() {
218   - return false;
219   - }
220   -
221   - @Override
222   - public E next() {
223   - return null;
224   - }
225   -
226   -
227   - @Override
228   - public boolean hasPrevious() {
229   - return false;
230   - }
231   -
232   - @Override
233   - public E previous() {
234   - return null;
235   - }
236   -
237   - @Override
238   - public int nextIndex() {
239   - return 0;
240   - }
241   -
242   - @Override
243   - public int previousIndex() {
244   - return 0;
245   - }
246   -
247   - @Override
248   - public void remove() {
249   -
250   - }
251   -
252   - @Override
253   - public void set(E e) {}
254   -
255   - @Override
256   - public void add(E e) {
257   -
258   - }
  226 + public ListIterator<E> listIterator() {
  227 + return new LinkedListIterator();
  228 + }
  229 + public ListIterator<E> listIterator(int index) {
  230 + if (index < 0 || index >= size) {
  231 + throw new IndexOutOfBoundsException();
  232 + }
  233 + /*ListIterator<E> twoWayLinkedListIterator = new LinkedListIterator(index);
  234 + for(int i = 0; i < index; i++) {
  235 + twoWayLinkedListIterator.next();
  236 + }
  237 + return twoWayLinkedListIterator;
  238 + */
  239 + return new LinkedListIterator(index);
259 240 }
260   -
  241 + @Override
  242 + public Iterator<E> iterator() {
  243 + return new LinkedListIterator();
  244 + }
  245 + private class LinkedListIterator implements java.util.ListIterator<E> {
  246 + private Node<E> current;
  247 + int index;
  248 + private boolean removeCalled,addCalled;
  249 +
  250 + public LinkedListIterator() {
  251 + index = 0;
  252 + current = head;
  253 + removeCalled = false;
  254 + }
  255 + public LinkedListIterator(int index) {
  256 + if (index < 0 || index >= size) {
  257 + throw new IndexOutOfBoundsException();
  258 + }
  259 + else if (index == 0) {
  260 + current = head;
  261 + }
  262 + else if(index == size-1) {
  263 + current = tail;
  264 + }
  265 + else {
  266 + for (int i = 0; i < index; i++)
  267 + current = current.next;
  268 + }
  269 + this.index = index;
  270 + }
  271 + public void setLast() {
  272 + current = tail;
  273 + }
  274 + @Override
  275 + public boolean hasNext() {
  276 + return (current != null);
  277 + }
  278 + @Override
  279 + public E next() {
  280 + if(current == null) {
  281 + throw new java.util.NoSuchElementException();
  282 + }
  283 + E e = current.element;
  284 + current = current.next;
  285 + removeCalled = false;
  286 + addCalled = false;
  287 + index++;
  288 + return e;
  289 + }
  290 + @Override
  291 + public boolean hasPrevious() {
  292 + return (current != null);
  293 + }
  294 + @Override
  295 + public E previous() {
  296 + if(current == null) {
  297 + throw new java.util.NoSuchElementException();
  298 + }
  299 + E e = current.element;
  300 + current = current.previous;
  301 + removeCalled = false;
  302 + addCalled = false;
  303 + index--;
  304 + return e;
  305 + }
  306 + @Override
  307 + public void set(E e) {
  308 + if (!addCalled && !removeCalled) {
  309 + current.element = e;
  310 + }
  311 + }
  312 +
  313 + // Extra uimplementerte metoder
  314 +
  315 + @Override
  316 + public int nextIndex() {
  317 + //if (!hasNext())
  318 + // return size;
  319 + if (index == size-1) {
  320 + return size;
  321 + }
  322 + else
  323 + return index+1;
  324 + }
  325 + @Override
  326 + public int previousIndex() {
  327 + //if (!hasPrevious())
  328 + // return -1;
  329 + if (index == 0) {
  330 + return -1;
  331 + }
  332 + else {
  333 + return index-1;
  334 + }
  335 + }
  336 + @Override
  337 + public void remove() {
  338 + if (current.previous == null || !removeCalled) {
  339 + throw new IllegalStateException();
  340 + }
  341 + else if (current.previous.previous == null) {
  342 + head = current;
  343 + current.previous.next = null;
  344 + }
  345 + else {
  346 + current.previous.previous.next = current;
  347 + current.previous = current.previous.previous;
  348 + }
  349 + size--;
  350 + removeCalled = true;
  351 + }
  352 + @Override
  353 + public void add(E e) {
  354 +
  355 + Node<E> newNode = new Node<>(e);
  356 + current.previous.next = newNode;
  357 + newNode.previous = current.previous;
  358 + current.previous = newNode;
  359 + newNode.next = current;
  360 +
  361 + index++;
  362 + size++;
  363 + addCalled = true;
  364 + }
  365 + }
261 366 private class Node<E> {
262 367 E element;
263 368 Node<E> next;
... ... @@ -267,23 +372,8 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
267 372 element = o;
268 373 }
269 374 }
270   -
271 375 @Override
272 376 public int size() {
273 377 return size;
274   - }
275   -
276   -
277   - public ListIterator<E> listIterator() {
278   - return null;
279   - }
280   -
281   - public ListIterator<E> listIterator(int index) {
282   - return null;
283   - }
284   -
285   - @Override
286   - public Iterator<E> iterator() {
287   - return null;
288   - }
289   - }
  378 + }
  379 + }
... ...