What happens when Front ==rear in circular queue
What happens when front==rear in circular queue
is queue have one element or is full or 开发者_如何学Pythonis empty
It's ambiguous. You need another mechanism to track whether the queue is full. See the Difficulties section on the Wikipedia page for a discussion.
To quote from that page, here are some ideas on how to solve it:
To solve this problem there are a number of solutions:
- Always keep one slot open.
- Use a fill count to distinguish the two cases.
- Use read and write counts to get the fill count from.
- Use absolute indices
.
Depends on where front and rear are pointing at: array elements, or objects somewhere on the heap. (You didn't mention your programming language.)
If in your programming language nil
doesn't equal nil
, your circular queue of objects will have one element. But if nil == nil
evaluates to true
... you can't tell :-)
In the case of a queue implemented via an array, you can't tell it either. Your queue may be full as well...
精彩评论