zoukankan      html  css  js  c++  java
  • 数据结构和算法基础之栈的顺序存储

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        /// <summary>
        /// 顺序栈
        /// </summary>
        public  class OrderStack<T>
        {
            public T[] DataArry;
            public int Top;
            public int MaxCapacity
            {
                get
                {
                    return DataArry.Length;
                }
            }
    
            public OrderStack(int stackLength)
            {
                DataArry = new T[stackLength];
                Top = 0;
            }
    
            /// <summary>
            /// 压栈
            /// </summary>
            public void PushStack(T data)
            {
                if(Top>=MaxCapacity)
                {
                    return;
                }
                Top++;
                DataArry[Top] = data;
            }
    
            /// <summary>
            /// 弹栈
            /// </summary>
            public T PopStack()
            {
                if(Top<0)
                {
                    return default(T);
                }
                T data = DataArry[Top];
                Top--;
                return data;
            }
    
            /// <summary>
            /// 查看栈顶元素
            /// </summary>
            /// <returns></returns>
            public T PeekStack()
            {
                if(Top==-1)
                {
                    return default(T);
                }
                return DataArry[Top];
                
            }
            /// <summary>
            /// 清空
            /// </summary>
            public void ClearStack()
            {
                Top = -1;
                for (int i = 0; i < DataArry.Length; i++)
                {
                    DataArry[i] = default(T);
                }
            }
        }
    }
  • 相关阅读:
    week02 线性表
    week01绪论
    第一周作业
    C语言第二次实验作业
    C语言实验报告
    博客作业06--图
    博客作业05--查找
    博客作业04--树
    博客作业03--栈和队列
    博客作业2---线性表
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10544460.html
Copyright © 2011-2022 走看看