加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 安全 > 正文

算法系列15天速成 第十天 栈

发布时间:2020-03-14 19:10:36 所属栏目:安全 来源:站长网
导读:今天跟大家聊聊栈,在程序设计中,栈的使用还是非常广泛的,比如有“括号匹配问题“,”html结构匹配问题“。所以说掌握了”栈“的使用,对我们学习算法还是很有

#region 栈是否已满
        /// <summary>
/// 栈是否已满
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
        public bool SeqStackIsFull<T>(SeqStack<T> seqStack)
        {
            return seqStack.top == seqStack.data.Length;
        }
        #endregion

#region 入栈
        /// <summary>
/// 入栈
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <param></param>
        public void SeqStackPush<T>(SeqStack<T> seqStack, T data)
        {
            if (SeqStackIsFull(seqStack))
                throw new Exception("不好意思,栈溢出");

seqStack.data[++seqStack.top] = data;
        }
        #endregion

#region 出栈
        /// <summary>
/// 出栈
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <returns></returns>
        public T SeqStackPop<T>(SeqStack<T> seqStack)
        {
            if (SeqStackIsEmpty(seqStack))
                throw new Exception("呜呜,栈已空");

seqStack.data[seqStack.top] = default(T);

return seqStack.data[--seqStack.top];
        }
        #endregion

#region 获取栈顶
        /// <summary>
/// 获取栈顶
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <returns></returns>
        public T SeqStackPeek<T>(SeqStack<T> seqStack)
        {
            if (SeqStackIsEmpty(seqStack))
                throw new Exception("栈已空");

return seqStack.data[seqStack.top];
        }
        #endregion

#region 获取栈中元素个数
        /// <summary>
/// 获取栈中元素个数
/// </summary>
/// <typeparam></typeparam>
/// <param></param>
/// <returns></returns>
        public int SeqStackLen<T>(SeqStack<T> seqStack)
        {
            return seqStack.top + 1;
        }
        #endregion
    }
}



(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读