Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.3 claims double-ended queue requires doubly-linked list #1641

Open
j-n-f opened this issue Feb 5, 2025 · 2 comments
Open

5.3 claims double-ended queue requires doubly-linked list #1641

j-n-f opened this issue Feb 5, 2025 · 2 comments

Comments

@j-n-f
Copy link

j-n-f commented Feb 5, 2025

Why does a double-ended queue require a doubly-linked list? This can be implemented with a singly-linked list

  • push front - insert before the head node
  • pop front - remove the head node, point to the following node
  • push back - append to the tail
  • pop back - remove the tail node

In all cases, you maintain a pointer to head and tail.

The only time a doubly-linked list would make sense is if you need to iterate through the entire list front-to-back and back-to-front.

@Xylphy
Copy link

Xylphy commented Feb 7, 2025

The key benefit with a doubly-linked list is for pop-back operations. Since each node knows its predecessor (via the prev pointer), you can easily access and remove the tail node in O(1) time without needing to traverse the entire list.

@j-n-f
Copy link
Author

j-n-f commented Feb 7, 2025

That makes sense (how you've explained it). I didn't see that when reading the text and viewing the diagrams.

I think the diagrams are a little confusing. The one for popLast() seems to show the head node being removed (instead of the tail, the head node is colored orange). It does make sense if you view all the diagrams in order. Maybe the diagram where the node is removed could show the removed node at half opacity, or show it disconnected from the rest of the list.

Thanks for the clarification 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants