The fundamental operations in a double-ended queue D are: insertFirst(e) - Insert a new element at the beginning of D. insertLast(e) - Insert a new element at the end of D. removeFirst() - Remove and return the first element of D. removeLast() - Remove and return the last element of D. In an empty double-ended queue, the following operations are performed: insertFirst(10) insertLast(32) a <- removeFirst() insertLast(28) insertLast(17) a <- removeFirst() a <- removeLast() The value of a is ______
GATE 2024 · Programming and Data Structures · Queue · medium
Answer: The value of a is 17.
- Simulate the first four operations: Start: Deque = [] insertFirst(10): Deque = [10] insertLast(32): Deque = [10, 32] a <- removeFirst(): removes 10 from front, a = 10, Deque = [32] insertLast(28): Deque = [32, 28]
- Simulate the remaining operations: insertLast(17): Deque = [32, 28, 17] a <- removeFirst(): removes 32 from front, a = 32, Deque = [28, 17] a <- removeLast(): removes 17 from rear, a = 17, Deque = [28] Final value of a = 17