How to store previous locations in a list to teleport?

Basically: I have a character I want to be able to use an ability and they will ‘snap’ back to a position 5 seconds ago.

I understand the concept of how to do this: check the players location every second, store it in a list with 5 indexes (one for each second). If the player presses the button, set their transform position to the oldest index.

What I’m having trouble with is figuring out the code to actually get the list functioning in a way that it saves only the latest three values rather than appending infinite numbers onto the end.

Any help would be appreciated!

using System.Collections.Generic;

private int maxLength = 20; // Oh what the heck, let's save 4 times per second ;)
private Queue<Vector3> positions = new Queue<Vector3>(maxLength+1);
private Queue<Quaternion> rotations= new Queue<Quaternion>(maxLength+1);

void Store(Transform trans)
{
  positions.Enqueue(trans.position);
  rotations.Enqueue(trans.rotation);
  if (positions.Count>maxLength)
  {
    positions.Dequeue();
    rotations.Dequeue();
  }
}

void Teleport(Transform trans)
{
  trans.position = positions.Peek();
  trans.rotation = rotations.Peek();
}

There is probably a more efficient way of doing it but because you already know it will be a max of 5 you can hard code a converter. So Make 2 arrays, a temp array and the actual array of locations.

When you add a new value to the “actual array” temporarily set all the current array values (except the one you are deleting) to the temp array. Delete the actual array then, and then make the temp array equal the actual array plus the current location.