how do i know whether the circular queue is full or empty?
how do i know whether the circular queue is full or empty?
One solution is a counter.
Kuphryn
what is a counter
any other solutions
1) Counter solution
The best one. All you need is an integer variable. Whenever you add an item to your queue, you need to increment it by one and vice versa: whenever you remove an item, you need to decrement the counter by one. Counter is initalized with 0, of course.
2) Solution w/o counter
You can calculate the number of items in your queue from the indexes of tail & head. It's a little tricky since tail can be < than head (the values of head and tail when the queue is empty should be taken into consideration too).
Conclusion:
Use solution #1