mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
fix remove method
This commit is contained in:
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user