Is there an existing data structure that is of fixed size, and will push the oldest/last element out if a new element is inserted?

I’m looking for a data structure that will push its oldest/last element out if a new element is inserted. For example, let D represent the structure. D contains 3 elements of the type Number D‘s default values will be initialized to 1, 2 and 3.

D = [1, 2, 3]

If a Number that contains the value 5 is inserted into D, 3 will be pushed out, while 1 and 2 are shifted right.

D = [5, 1, 2]

The first thing that comes to mind would be an array, but the definition does not include the pushing behavior.


Fixed-size queues are often implemented using what some people call circular buffers. If you remove the protection against it being full, you get the desired behaviour.

Of course, no actual pushing will happen in the array — that would be too expensive — but it will look like it from the outside.

Source : Link , Question Author : Greg M , Answer Author : Raphael

Leave a Comment