public static void Main(string[] args)
StackWithMax s = new StackWithMax(5);
private static void TestPush(StackWithMax s, int v)
Console.WriteLine("Push({0})", v);
Console.WriteLine(" Max = {0}", s.MaxValue());
private static void TestPop(StackWithMax s)
Console.WriteLine("Pop->{0}", s.Pop());
Console.WriteLine(" Max = {0}", s.MaxValue());
public class StackWithMax
public StackWithMax(int size)
numbers = new Stack(size);
maximums = new Stack(size);
public void Push(int number)
maximums.Push(maximums.IsEmpty() ? number : Math.Max(number, maximums.Top()));
return numbers.IsEmpty();
if (count >= data.Length)
throw new ApplicationException("Stack overflow");
throw new ApplicationException("Stack underflow");
throw new ApplicationException("Stack underflow");