Queues Flashcards
1
Q
what’s the order principle of queue
A
first in first out
2
Q
how to implement queue using linkedlist
A
- enqueue at the tail by adding to the back
- dequeue at the head by removing from the head, because removing only costs O(1) when removing form the head
3
Q
how to implement queue using arrays
A
- use a wrap-around array
- need a front and a back index tracker for optimization
- back index = last item’s index + 1
- enqueue at the back by adding to the back
- dequeue at the front by removing from the head
4
Q
linkedlist backed queue time complexity
- enqueue
- dequeue
- check empty
- get size
- clear
- resize
A
- enqueue: O(1)
- dequeue:O(1)
- check empty: O(1)
- get size: O(1)
- clear: O(1)
- resize: O(1)
5
Q
array backed queue time complexity
- enqueue
- dequeue
- check empty
- get size
- clear
- resize
A
- enqueue: O(1)*, because of resizing
- dequeue:O(1)
- check empty: O(1)
- get size: O(1)
- clear: O(1)
- resize: O(n)
6
Q
double ended queue (deque) vs. queue
A
- deque makes adding and removing from both ends efficient
- deque can be implemented using arrays and linkedlists