• c#自定义栈


    栈的原理是先进后出。队列是先进先出。栈像乘坐电梯,后进的先出去。队列就是排队。

    实现代码:

      public class myStack<T>
        {
            private int top;
            private T[] stkArr;
            private int maxSize;
    
            /// <summary>
            /// 表示栈顶索引
            /// </summary>
            public int Top
            {
                set { top = value; }
                get { return top; }
            }
    
            public T[] StkArr
            {
                set { stkArr = value; }
                get { return stkArr; }
            }
    
            public int MaxSize
            {
                get { return maxSize; }
                set
                {
                    if (value < 0)
                    {
                        throw new Exception("maxSize is d 0!");
                    }
                    else
                    {
                        maxSize = value;
                    }
                }
            }
    
            public myStack(int maxSize)
            {
                this.MaxSize = maxSize;
                this.StkArr = new T[maxSize];
                Top = 0;
            }
            /// <summary>
            /// 出栈
            /// </summary>
            public T pop()
            {
                if (isNull())
                {
                    throw new Exception("栈为空!");
                }
                else
                {
                    return StkArr[--Top];
                }
            }
    
            /// <summary>
            /// 入栈
            /// </summary>
            public void pash(T value)
            {
                if (isFull())
                {
                    throw new Exception("栈已满!");
                }
                else
                {
                    StkArr[top++] = value;
                }
            }
    
            /// <summary>
            /// 判断栈是否已满
            /// </summary>
            /// <returns></returns>
            public bool isFull()
            {
                return (Top == MaxSize);
            }
    
            /// <summary>
            /// 判断栈是否为空
            /// </summary>
            /// <returns></returns>
            public bool isNull()
            {
                return (Top == 0);
            }
    
            /// <summary>
            /// 返回栈大小
            /// </summary>
            /// <returns></returns>
            public int getMaxSize()
            {
                return MaxSize;
            }
    
            /// <summary>
            /// 遍历栈
            /// </summary>
            /// <returns></returns>
            public IEnumerable getEnumerable()
            {
                for (int i = 0; i < MaxSize; i++)
                {
                    yield return stkArr[i];
                }
            }
        }
    

      

  • 相关阅读:
    java第四次作业
    JAVA第三次作业
    Java第二次作业
    Java第一次作业
    android studio下载
    Android面试常问到的知识点
    [Java基础]List,Map集合总结
    搞ACM的你伤不起[转自RoBa]------(看一次,笑一次)
    06.5.1.Functions
    05.virsh命令的常用操作(kvm)
  • 原文地址:https://www.cnblogs.com/sjyzz/p/7625340.html
Copyright © 2020-2023  润新知