retagged by
15,390 views
36 votes
36 votes

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let $n$ denote the number of nodes in the queue. Let 'enqueue' be implemented by inserting a new node at the head, and 'dequeue' be implemented by deletion of a node from the tail.

Which one of the following is the time complexity of the most time-efficient implementation of 'enqueue' and 'dequeue, respectively, for this data structure?

  1. $\Theta(1), \Theta(1)$
  2. $\Theta(1), \Theta(n)$
  3. $\Theta(n), \Theta(1)$
  4. $\Theta(n), \Theta(n)$
retagged by

6 Answers

0 votes
0 votes

Answer is B:

 In question there is a Single linked list of n nodes which is given. In this Linked list backward traversing is not possible. and the queue is FIFO data structure.

1)For  Enqueue:This will take θ(1) because inserting a new node at the head(first node) will take constant time.

1)For  Dequeue: In queue deletion of first inserted element, it will take θ(n) time to trace the path from the last node to the first node for deletion

 

 

 

0 votes
0 votes
It is (B) if we use simple Linked List, and constant time if a doubly Linked List would have been given.
Answer:

Related questions