GATE CS 2018 · Asymptotic Analysis & Recurrences

MCQ · 1 mark · Problem PROG-DIVC-0179

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer: the head points to the first node, each node points to the next one, and the tail points to the last node. 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. A
    \(\Theta (1), \Theta (1)\)
  2. B
    \(\Theta (1), \Theta (n)\)
  3. C
    \(\Theta (n), \Theta (1)\)
  4. D
    \(\Theta (n), \Theta (n)\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{\text{Deleting a node from a singly linked list needs the node before it}}\]Enqueue: link the new node in front of the current head and move the head pointer: \(\Theta(1)\).Dequeue: removing the tail node needs the node before it (to become the new tail), and that node can be reached only by walking from the head: \(\Theta(n)\).Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com