Skip to content

Instantly share code, notes, and snippets.

@Dkowald
Last active June 14, 2017 20:14
Show Gist options
  • Select an option

  • Save Dkowald/5e6c53d058a02fb4ee5a972e63c3b933 to your computer and use it in GitHub Desktop.

Select an option

Save Dkowald/5e6c53d058a02fb4ee5a972e63c3b933 to your computer and use it in GitHub Desktop.
Simple length-limited stack
namespace kwd.gist {
using System.Collections.Generic;
public class CircularStack<T>
{
private readonly int _max;
private readonly List<T> _items = new List<T>();
public CircularStack(int max)
{
_max = max;
}
public void Push(T newItem)
{
_items.Insert(0, newItem);
if(_items.Count >= _max){ _items.RemoveAt(_items.Count-1);}
}
public IReadOnlyList<T> Items => _items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment