/** ************************************************* Skrá: T203Sd4_3.cs Höfundur: Ragnar Geir Brynjólfsson. Dagsetning: 06.06.2005. Stafli útfærður með Stack ************************************************* */ using System; using System.Collections; class MyStackException : SystemException { public MyStackException(string err):base(err) {} } class MyStack { private const int MAX = Int32.MaxValue; private Stack S = new Stack(); private int topp = -1; public int size(){ return topp+1; } public bool isEmpty(){ return (topp<0); } public bool isFull() { return (S.Count==MAX); } public Object top() { if(isEmpty()) throw new MyStackException("Stafli tómur."); return S.Peek(); } public void push(Object o) { if (isFull()) throw new MyStackException("Stafli fullur."); S.Push(o); topp++; } public Object pop() { if(isEmpty()) throw new MyStackException("Stafli tómur."); topp--; return S.Pop(); } public override string ToString() { String utstr="[ "; IEnumerator me = S.GetEnumerator(); while(me.MoveNext()) utstr+=me.Current.ToString()+" "; utstr+="]"; return utstr; } } class T203Sd4_3 { static void Main() { MyStack s = new MyStack(); s.push(1.23); s.push(2.14); Console.WriteLine(s.ToString()); Console.WriteLine(s.pop()); Console.WriteLine(s.pop()); } }