views:

129

answers:

2

What happens when front==rear in circular queue

is queue have one element or is full or is empty

+5  A: 

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

.

tvanfosson
+1 for sheer speed (and the correct answer of course) ;-)
ChristopheD
I am asking that what happens if the front and rear are equal in the queuei.e in the queue of length 5if rear stars from -1 and front starts from 0 then if (front==rear)what happens..
m.qayyum
A: 

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...

jschulenklopper
i get it...Thanks
m.qayyum