• 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;
            }
        }
  • 相关阅读:
    git的使用
    本体建模
    word2vec改进之Negative Sampling
    word2vec改进之Hierarchical Softmax
    word2vec原理
    Window下mysql的安装
    PageRank算法
    集成学习-------简单介绍
    自我介绍
    Apollo学习笔记(二):循迹实现过程
  • 原文地址:https://www.cnblogs.com/kangs/p/3112793.html
Copyright © 2020-2023  润新知