Simple method to implement a FIFO (first-in-First-out)
Use an array to hold the data. A head, and tail index is used manages the FIFO.
If the FIFO get overrun, then the oldest element (FI) get deleted.
Maximum number of elements the FIFO can hold is is size_of_array-1
head points to the next "free" space.
tail points to the end of queue (last item). Unless, tail==head, then queue is empty.
0. Initialize
head = tail = 0;
N = number of array elements .
2. n - caculate the number of items in the array
if( head < tail )
n = N - (tail-head)
else
n = (head-tail)
1. add data to FIFO
copy to head
inc(head)
if( tail== head ) // FIFO is being overrun.
inc(tail)
3. remove data
if( head==tail) error FIFO is empty.
copy tail
inc(tail)
4. inc( x, N) - increment head, tail.
if( x >= N-1 )
x = 0;
else
x++;