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 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?
View worked solution
Answer: B
To determine the time complexity of the enqueue and dequeue operations, let's analyze how they are performed on the given non-circular singly linked list:
1. Enqueue Operation (Insertion at the Head)
To insert a new node at the head of the singly linked list:
- Create a new node.
- Set the
nextpointer of the new node to point to the currentheadnode:new_node->next = head; - Update the
headpointer to point to the new node:head = new_node;
Since we have direct access to the head pointer, this operation does not require traversing the list. Thus, the time complexity of enqueue is .
2. Dequeue Operation (Deletion from the Tail)
To delete the node at the tail of the singly linked list:
- We need to update the
tailpointer to point to the second-to-last node (the predecessor of the current tail). - Because this is a singly linked list, there are no backward pointers. To find the predecessor of the tail, we must start at the
headand traverse the entire list until we find the node whosenextpointer points totail. - This traversal requires visiting nodes, which takes linear time.
Thus, the time complexity of dequeue is .
Therefore, the time complexities of enqueue and dequeue are and , respectively.
Free practice · Your answers on this page are not saved.
