The fundamental operations in a double-ended queue D are:
insertFirst(e) – Insert a new element e at the beginning of D.insertLast(e) – Insert a new element e 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 ______.
Answer
17
Explanation
Trace the deque, writing its front on the left:
insertFirst(10): [10]insertLast(32): [10, 32]removeFirst()returns 10, so a = 10: [32]insertLast(28)andinsertLast(17): [32, 28, 17]removeFirst()returns 32, so a = 32: [28, 17]removeLast()returns 17, so a = 17: [28]
Each assignment overwrites a, so its final value is 17. The 28 is still in the deque and is never assigned.
Answer: 17