I am trying to code a FIFO queue which takes data in and once full releases the oldest piece of data to make room for the new data.
I am new at programming but have managed to come up with the following code:
int Q[size], f=0, r=-1;
int Qfull()
{
if (r==size) return 1;
return 0;
}
int Qinsert()
{
if(Qfull())
{
elem=Q[f];
f=f+1;
return elem;
r++;
Q[r]=SPI1BUF;
}
else
{
r++;
Q[r]=SPI1BUF;
}
}
The problem i am having is that this does not shift data and will fail once the array is full due to r increasing past the array size. Is there any way to solve this?
Q+size, set it toQ.next = (next + 1) % size;.)