Reputation: 45
I am trying to print a doubly linked list starting with the tail element and ending with the first. My current code below does that but is also returning the dequed items for some reason. When I print the list from the head to the tail, it doesn't do this. Idk if it's the toString causing this or the dequed methods. I have included both.
public String toString() {
String result;
if (isEmpty())
return "empty";
else {
result = "";
DoubleNode current = tail;
while (current != null) {
result = result + current.getElement() + " ";
current = current.getPrev();
}
}
return result;
}
public Item dequeueBack() throws NoSuchElementException {
if (isEmpty())
throw new NoSuchElementException("deque is empty");
Item result = tail.getElement();
tail = tail.getPrev();
count--;
if (isEmpty())
head = null;
else
tail.setNext(null);
return result;
}
Upvotes: 0
Views: 429
Reputation: 49803
You don’t setPrev anything when you dequeue, so those links (which you print is based on) still work.
Upvotes: 1