Queues Flashcards

1
Q

what’s the order principle of queue

A

first in first out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly