in DS edited by
335 views
4 votes
4 votes

Suppose you implement a queue using a singly linked list with head and tail pointers so that the front of the queue is at the tail of the list, and the rear of the queue is at the head of the list.

What is the best possible worst-case running time for enqueue and dequeue in this situation? (As a reminder, enqueue occurs at the rear of the queue.)

  1. $O(1)$ for both functions.
  2. $O(1)$ for enqueue and $O(n)$ for dequeue.
  3. $O(n)$ for enqueue and $O(1)$ for dequeue.
  4. $O(n)$ for both functions.
in DS edited by
335 views

1 Answer

1 vote
1 vote
In the time of enqueuing we are using tail pointer to direct insert the node in rear

so it takes O(1) in worst .

But in the time if dequeuing we are traversing that tail pointer to the first node where head already exists..so now to delete or dequeue the element we need to traverse tail pointer to whole SLL to get that front pointer node in queue. SO O(n) in worst case.