I’m starting this serie with the circular buffer. The basic idea of this data structure is to maintain a fixed size buffer Old data is overwritten by new data and retrieval is done in an First In First Out order.
For an example, lets consider a circular buffer of size 5. lets perform insertion from 0 to 4 into it. Since it is circular, any position could be used for the first insertion.
Now that the all the insertions are done, there is no more space left in the buffer. Lets say we insert 5 in the buffer.
The oldest data (i.e. 0) have been overwritten by the insertion.
In the same way, when trying to retrieve data, the oldest data is retrieved first (in this particular case 1 since 0 is overwritten by 5).
Question is what happens if we try to get data while the buffer is empty or doesn’t have enough elements, I think this will depend on the implementation and the application.
Implementation details can be found here.