using System.Collections; using System.Collections.Generic; /// /// Extended version of List used to give stack functionality, but with a maximum capacity. /// public class CappedStack : List { public int maxValues; public CappedStack(int max) { maxValues = max; } /// /// Adds the object to the top of the stack. If stack is longer than maxValues, /// removes the oldest item. /// /// Object to go on top of the stack. public void Push(T val) { Add(val); if (Count > maxValues) { RemoveAt(0); } } /// /// Returns the item on top of the stack and removes it. /// /// Top stack item. public T Pop() { T val = this[Count - 1]; RemoveAt(Count - 1); return val; } }