GATE / CS ARCHIVE

GATE CSE 2018 / QUESTION 3

Queue operations on a singly linked list

Data Structures · Arrays & Linked Lists · Full GATE CSE 2018 paper

GATE 2018, Q3 1 mark · MCQ
Report issue

Arrays & Linked Lists

Question 3, GATE 2018: A noncircular singly linked queue stores head and tail pointers. Enqueue inserts at head; dequeue removes tail. For n nodes, choose the optimal worst-case time pair: A Θ(1),Θ(1); B Θ(1),Θ(n); C Θ(n),Θ(1); D Θ(n),Θ(n).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Method

Enqueue sets new.next=head and head=new, also initializing tail if previously empty. Tail removal must change the predecessor’s next pointer to null and make it the new tail. A tail pointer identifies the node to remove but not its predecessor, because links point only forward. Scanning from head is unavoidable with only these stored pointers. Reversing the operation orientation would allow constant-time deletion at head, but that is not this question’s data structure.

Solution

Inserting at the head updates a constant number of pointers, Θ(1). Removing the tail requires locating its predecessor, which a singly linked list with only head/tail pointers does not directly supply; traversing n nodes costsΘ(n) in the worst case. Thus B.

Original paper, page 4 · Answer key