The key definition of a stack is LIFO A queue is somewhat related but the key concept is First In, First Out, FIFO The word queue must be of French origin given its spelling. In New York people stand on line at the theater, in America, people stand in line and in England, they queue up.
A queue needs two pointers, front and rear. It also uses four routines like stacks. Items are added at the rear and removed at the front.
Queues are normally used in operating systems for scheduling various tasks. Often there is more than one for a given resource to allow priorities. They can be used as a kind of buffer, for message passing between processes and in simulations.
The first implementation we will see is using an array. The front and rear pointers are simple integer variables containing the subscripts into the queue array. Front and rear are initialized to 0. Rear is incremented before storing and Front is incremented before retrieval.
These routines assume an array of items, numbered from 1 to QSIZE named Q and two variables front and rear, both initialized to 0.
Notice what happens if many things are added to the queue so that the rear pointer points to the last element in the array but the front has moved from the first subscript. There are empty positions in the array but we can't use them because of the location of the rear pointer.
Whenever the front and rear pointers are equal, the queue is empty. If we set front = rear = 0 at this point, the queue is still empty and we can reuse the space in the array. This is because we have returned to the initial conditions, where the front and rear are equal This only puts off the problem, it doesn't solve it. The following section presents a technique to solve this.
By using modulo arithmetic to increment the pointers instead of regular integer arithmetic, we can wrap the pointers around the ends of the array and thus use all the available space. Modulo arithmetic works as follows:
a mod b
returns the remainder of a divided by b Thus, a mod b will give a value between 0 and b-1.
`These routines assume an array of items, numbered from 0 to QSIZE-1 named Q and two variables front and rear, both initialized to 0.
A space is left open in the circular queue because the check for empty and full is the same. If the check is changed, this space can be used for data. This check can be the same since it is done at different times. When full, the rear pointer has come around the queue and caught up with the front pointer. When empty, the front pointer has caught up with the rear pointer.
Notice what happens when the queue is in fact full and you try to add an item. The rear pointer is incremented and then we check for full and it is true. Now if we try to remove one, it fails because it is also empty. The two blank lines in the code above are to handle this problem. The first should get the line
oldrear = rearand the second is
rear = oldrearThere are other ways to undo the increment of rear but this is probably the easiest.