- #1
magnifik
- 360
- 0
i am using dummy nodes for a doubly linked list and trying to get an object at the specified index. for whatever reason, when i test it, i am getting data but it is backwards. for example, when i have a list of integers from 0 to 21, and i attempt to call get(2), i get 19 which is at index 2 starting at the back of the list. since i am setting the cursor to be at the head, i am not sure what is causing this.
public Object get(int index) {
if (index < 0 || index > size()-1) throw new IndexOutOfBoundsException();
Node cursor = _head._next;
for (int i = 0; i < index; i++)
cursor = cursor._next;
return cursor._data;
}
public Object get(int index) {
if (index < 0 || index > size()-1) throw new IndexOutOfBoundsException();
Node cursor = _head._next;
for (int i = 0; i < index; i++)
cursor = cursor._next;
return cursor._data;
}