How do I find the number of items in a circular queue? |front - rear| doesn't always work.
Is there one formula to know how many elements are there in a circular queue using front, rear and size of the array?
two pointers FRONT and REAR.
we need to add... System. out. print(" "+arr[rear]); ... after while loop end.
CPU Scheduling: The operating system also uses the circular queue to insert the processes and then execute them. Traffic system: In a computer-control traffic system, traffic light is one of the best examples of the circular queue. Each light of traffic light gets ON one by one after every jinterval of time.
actually the size would be,
size = front > rear ? (MAX - front + rear + 1) : (rear - front + 1);
or one can go for a generic formula:
size = abs(abs(MAX - front) - abs(MAX -rear));//this works in every situation
Assuming you implement it using an array with size N
so there are pointers pointing to the front and rear. Use the following formula:
size = front > rear ? (front - rear) : (front+N - rear);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With