c#中栈的定义、操作

       class Stack
        {
            int maxsize;        //顺序栈的容量
            object[] data;      //数组,用于存储栈中的数据
            int top;            //指示栈顶


            public object this[int index]
            {
                get { return data[index]; }
                set { data[index] = value; }
            }
            //使用构造器初始化栈
            public Stack(int size)
            {
                data = new object[size];
                maxsize = size;
                top = -1;
            }
            //求栈的长度(栈中的元素个数)
            public double StackLength()
            {
                return top + 1;


            }


            //判断顺序栈是否为空
            public bool IsEmpty()
            {
                if (top == -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //判断顺序栈是否为满
            public bool IsFull()
            {
                if (top == maxsize - 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //入栈操作
            public void Push(object e)
            {
                if (IsFull())
                {
                    return;
                }
                data[++top] = e;
            }
            //出栈操作,并返回出栈的元素
            public object Pop()
            {
                object temp = null;
                if (IsEmpty())
                {
                    return temp;
                }
                temp = data[top];
                top--;
                return temp;
            }


        }

发布了25 篇原创文章 · 获赞 9 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章