What to choose to implement deque?
What would you rather choose to imp开发者_如何学Pythonlement deque: HashSet or LinkedList. And could you state cons and pros for both please? Thank you.
Of course LinkedList. All Dequeue operations there are implemented strictly as O(1) and no excessive memory is used.
A HashSet
is not a Deque
so you would have to use a LinkedList
(which does implement Deque
). The reason behind this is that HashSet
is not an ordered data structure, and hence cannot be used as a queue.
For thread-safe blocking implementations of Deque
consider LinkedBlockingDeque
or ArrayDeque
.
HashSet is not an alternative, because a set does not record the order in which the element was added. Additionally, a set does not allow the same element to be added more than once.
LinkedList is a better option or why not using an existing Deque implementation?
精彩评论