I'm trying to create a circular queue and come across this problem.
As you see, my answer is incorrect. I still don't understand the reason why it is the case, so let's go through everything again.
- Create a circular queue of size 2
- Enqueue 8, current queue: 8
- Enqueue 8, current queue: 8, 8
- Front: 8 (no change to the queue)
- Enqueue 4, current queue: 4, 8
- Dequeue, current queue: 8
- Enqueue 1, current queue: 8, 1
- Enqueue 1, current queue: 1, 1
- Rear: 1 (no change to the queue)
- Is empty: false (no change to the queue)
- Front : 1 (no change to the queue)
- Dequeue, current queue: 1
Following my analysis, the last Front
operation should return 1 as the queue is 1, 1
. However, the answer is 8 instead. Did I miss something? Please explain this for me.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…