If i got your question correct,i guess, you need to construct a circular buffer. This can be done using array functions. You can think of circular buffer as an array which stores a fixed number of data points(latest n samples). It works on principle of FIFO. Elements are added from the top of array and when the size is achieved, the first element that was added gets removed from the bottom of buffer. So you need to write a logic to acomplish this. Unfortunately at present i do not have an example handy. I hope you will be able to construct it yourself.
All the Best !