The operations for a queue are defined as below.
- ENQ n: This operation appends data n to the queue.
- DEQ: This operation removes data from the queue.
On an empty queue, the operations ENQ 1, ENQ 2, ENQ 3, DEQ, ENQ 4, ENQ 5, DEQ, ENQ 6, DEQ, DEQ are performed. After that, when DEQ is performed, what is the value that is removed?
Explanation:
Let's walk through the operations step by step on an empty queue.
- ENQ 1: Queue = [1]
- ENQ 2: Queue = [1, 2]
- ENQ 3: Queue = [1, 2, 3]
- DEQ: Removes 1 → Queue = [2, 3]
- ENQ 4: Queue = [2, 3, 4]
- ENQ 5: Queue = [2, 3, 4, 5]
- DEQ: Removes 2 → Queue = [3, 4, 5]
- ENQ 6: Queue = [3, 4, 5, 6]
- DEQ: Removes 3 → Queue = [4, 5, 6]
- DEQ: Removes 4 → Queue = [5, 6]
Now, when the next DEQ is performed, the value that is removed is 5.
So, the value removed by the next DEQ is 5.
Comments
Post a Comment