fix remove method

This commit is contained in:
Khwanchanok Srimool
2018-05-20 03:39:26 +07:00
parent c4a8e1e18c
commit a85f2525b4

View File

@ -34,7 +34,7 @@ public class CircleLinkedList<E>{
} }
public E remove(int pos){ public E remove(int pos){
if(pos>size || pos< 0){ if(pos>=size || pos< 0){
//catching errors //catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
} }
@ -45,13 +45,15 @@ public class CircleLinkedList<E>{
iterator = iterator.next; iterator = iterator.next;
before = before.next; before = before.next;
} }
E saved = iterator.value; E removedValue = iterator.value;
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head. // assigning the next reference to the the element following the element we want to remove... the last element will be assigned to the head.
before.next = iterator.next; before.next = iterator.next;
// scrubbing // scrubbing
iterator.next = null; iterator.next = null;
iterator.value = null; iterator.value = null;
size--;
return saved; return removedValue;
} }
} }