泛型栈

class MyStack<T>
    {
        T[] StackArray;
        int StackPointer = 0;
        public void Push(T x)
        {
            if (!IsStackFull)
            {
                StackArray[StackPointer++] = x;
            }
        }

        public T Pop()
        {
            return (!IsStackEmpty) ? StackArray[--StackPointer] : StackArray[0];
        }


        const int MaxStack = 10;
        bool IsStackFull
        {
            get { return StackPointer >= MaxStack; }

        }

        bool IsStackEmpty
        {
            get
            {
                return StackPointer <= 0;
            }
        }

        public MyStack()
        {
            StackArray = new T[MaxStack];

        }
        public void Print()
        {
            for (int i =  StackPointer-1; i >=0; i--)
            {
                Console.WriteLine(StackArray[i]);
            }
        }
    }

 

posted @ 2015-06-03 20:18  Geek0007  阅读(202)  评论(0编辑  收藏  举报