A priority queue is used to implement a stack that stores characters. PUSH (C) is implemented as INSERT where is an appropriate integer key chosen by the implementation. POP is implemented as DELETEMIN. For a sequence of operations, the keys chosen are in
View worked solution
Answer: D
A stack operates on the Last-In-First-Out (LIFO) principle, meaning the element inserted last must be the first one to be removed.
In the given implementation:
PUSH(C)is implemented asINSERT(Q, C, K).POP()is implemented asDELETEMIN(Q), which retrieves the element with the minimum key value.
To ensure that the most recently pushed element has the minimum key (so it gets popped first), each new element pushed must be assigned a key that is strictly smaller than the keys of all previously pushed elements.
For example, if we push elements in sequence, their assigned keys must satisfy:
This ensures that (the last pushed element with the minimum key ) is popped first by DELETEMIN(Q).
Thus, the keys chosen for successive push operations must be in strictly decreasing order.
Free practice · Your answers on this page are not saved.
