Queue

  • "First In, First Out" (FIFO)
  • enqueue(element): Add element to the back of the Queue peek(): Look at the element at the front of the Queue dequeue(): Remove the element at the front of the Queue
  • We can use a Deque to implement Queue. The Deque would have its own backing structure of either a Doubly-Linked List or a Circular Array, because as you should recall, a Deque is also an ADT
  • How to use circular array to create a queue?
Ans: keep int head, tail, to record the start and the end of this queue.
  • How to use Linked List to create a queue?
Ans: 
Keep head and tail pointer, point to the start and the end of this queue. 
When add node to queue, create a new node and tail point to it.
When remove node from queue, head point to next node.
  • What is the worst-case time complexity of adding an element into a Queue, given that we are using a Deque as our backing structure in our implementation and given that we are using a Circular Array as the backing structure of our Deque?
Ans: O(n)
  • What is the worst-case time complexity of adding an element into a Queue, given that we are using a Deque as our backing structure in our implementation and given that we are using a Doubly-Linked List as the backing structure of our Deque?
Ans: O(1)
  • BFS
REFERENCE: Stepik 2.7

results matching ""

    No results matching ""