Skip to content
Official PYQ · 2018

GATE CS 2018 Q3 — Arrays, Stacks and Queues

Explain with AI

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 nn 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?

Choose one option
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:

  1. Create a new node.
  2. Set the next pointer of the new node to point to the current head node:
    new_node->next = head;
  3. Update the head pointer 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 Θ(1)\Theta(1).

2. Dequeue Operation (Deletion from the Tail)

To delete the node at the tail of the singly linked list:

  1. We need to update the tail pointer to point to the second-to-last node (the predecessor of the current tail).
  2. Because this is a singly linked list, there are no backward pointers. To find the predecessor of the tail, we must start at the head and traverse the entire list until we find the node whose next pointer points to tail.
  3. This traversal requires visiting n−1n-1 nodes, which takes linear time.

Thus, the time complexity of dequeue is Θ(n)\Theta(n).

Therefore, the time complexities of enqueue and dequeue are Θ(1)\Theta(1) and Θ(n)\Theta(n), respectively.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions