zoukankan      html  css  js  c++  java
  • C# 栈的实现

    早前写得栈的实现,基本功能都有。

    代码:

        /// <summary>
        ////// </summary>
        public class Stack
        {
            private object[] data; //用data数组来储存数据
    
            private int size;      //栈的大小
    
            private int top;       //top指针
    
            public object this[int loc]
            {
                get { return loc >= 0 && loc <= top ? data[loc] : null; }
            }
    
            /// <summary>
            /// 当前栈中元素个数
            /// </summary>
            public int Length
            {
                get { return this.top + 1; }
            }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="size"></param>
            public Stack(int size)
            {
                if (size>=0)
                {
                    this.data = new object[size];
                    this.size = size;
                    this.top = -1;        //初始top指针赋-1
                }
                else
                {
                    this.data = null;
                    this.size = 0;
                    this.top = -1;
                }
            }
            
            /// <summary>
            /// 是否空
            /// </summary>
            /// <returns></returns>
            public bool isEmpty()
            {
                return this.top == -1;
            }
    
            /// <summary>
            /// 是否满
            /// </summary>
            /// <returns></returns>
            public bool isFull()
            {
                return this.top == this.size - 1;
            }
    
            /// <summary>
            /// 压栈
            /// </summary>
            /// <param name="elem"></param>
            public void Push(object elem)
            {
                if (this.isFull())
                {
                    throw new Exception("Stack is full!");
                }
                this.top++;
                this.data[this.top] = elem;
            }
    
            /// <summary>
            /// 出栈
            /// </summary>
            /// <returns></returns>
            public object Pop()
            {
                if (this.isEmpty())
                {
                    throw new Exception("Stack is empty!");
                }
                object elem = this.data[this.top];
                this.top--;
                return elem;
            }
    
            /// <summary>
            /// 获取栈顶元素
            /// </summary>
            /// <returns></returns>
            public object getTop()
            {
                if (this.isEmpty())
                {
                    throw new Exception("Stack is empty!");
                }
                return this.data[this.top];
            }
    
            /// <summary>
            /// 清空
            /// </summary>
            public void Clear()
            {
                this.top = -1;
            }
        }
  • 相关阅读:
    HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)
    2018年暑假ACM个人训练题6 解题报告
    HDU 2096 小明A+B(%的运用)
    HDU 2097 sky数 (进制转化)
    布局(codevs 1242)
    Remmarguts’ Date(poj 2449)
    魔法猪学院(codevs 1835)
    统计单词个数(codevs 1040)
    小L 的二叉树(洛谷 U4727)
    Shortest Prefixes(poj 2001)
  • 原文地址:https://www.cnblogs.com/kangs/p/3112793.html
Copyright © 2011-2022 走看看