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

编程学习 2021-07-05 12:58www.dzhlxh.cn编程入门
今天跟大家聊聊栈,在程序设计中,栈的使用还是非常广泛的,比如有“括号匹配问题“,”html结构匹配问题“。所以说掌握了”栈“的使用,对我们学习算法还是很有帮助的


一: 概念

         栈,同样是一种特殊的线性表,是一种Last In First Out(LIFO)的形式,现实中有很多这样的例子,

     比如:食堂中的一叠盘子,我们只能从顶端一个一个的取。

 

二:存储结构

        ”栈“不像”队列“,需要两个指针来维护,栈只需要一个指针就够了,这得益于栈是一种一端受限的线性表。

      这里同样用”顺序结构“来存储这个”栈“,top指针指向栈顶,所有的操作只能在top处。

         

代码段:

代码如下:

#region 栈的数据结构
    /// <summary>
/// 栈的数据结构
/// </summary>
    public class SeqStack<T>
    {
        public T[] data;

        /// <summary>
/// 栈顶指针
/// </summary>
        public int top = -1;

        public SeqStack(int lenth)
        {
            data = new T[lenth];
        }
    }
    #endregion


三:常用操作

        栈的操作有:①初始化栈,②入栈,③出栈,④获取栈顶。

1: 初始化栈

        这个还是比较简单的,初始化栈时,设置默认top指针为-1,这个就不用图来展示了。

代码段:

代码如下:

#region 栈的初始化操作
        /// <summary>
/// 栈的初始化操作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
        public SeqStack<T> SeqStackInit<T>(int length)
        {
            SeqStack<T> seqStack = new SeqStack<T>(length);

            seqStack.top = -1;

            return seqStack;
        }
        #endregion

2:入栈

       这个操作主要就是做两件事情:① 将元素从栈顶压入,② top指针自增。


代码段:

代码如下:

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

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

3:出栈

      同样跟“入栈”类似,需要做两件事情,①干掉top处的元素,②top指针自减。

代码段

代码如下:

#region 出栈
        /// <summary>
/// 出栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="seqStack"></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

4:获取栈顶元素

      这个很简单,跟“出栈”唯一不同的是不破坏栈顶元素,只是翻出来看看而已。

代码段

代码如下:

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

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

总的运行代码如下

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeqStack
{
    class Program
    {
        static void Main(string[] args)
        {
            SeqStackClass stackManager = new SeqStackClass();

            SeqStack<Student> seqStack = stackManager.SeqStackInit<Student>(10);

            Console.WriteLine("********************  压入ID=1,ID=2,ID=3的元素  ***********************\n");
            //压入ID=1,ID=2,ID=3的元素
            stackManager.SeqStackPush(seqStack, new Student() { ID = 1, Name = "一线码农", Age = 23 });
            stackManager.SeqStackPush(seqStack, new Student() { ID = 2, Name = "huangxincheng520", Age = 23 });
            stackManager.SeqStackPush(seqStack, new Student() { ID = 3, Name = "51cto", Age = 23 });

            Console.WriteLine(".... 压入成功,当前栈中元素有:" + stackManager.SeqStackLen(seqStack) + "个");

            Console.WriteLine("\n******************  查看栈顶元素  ********************");

            var result = stackManager.SeqStackPeek(seqStack);

            Console.WriteLine("栈顶元素为:ID=" + result.ID + ",Name=" + result.Name + ",Age=" + result.Age);

            Console.WriteLine("\n********************  弹出栈顶元素  ***********************");

            stackManager.SeqStackPop(seqStack);

            Console.WriteLine("\n******************  查看栈中的元素  ********************");

            for (int i = 0; i < stackManager.SeqStackLen(seqStack); i++)
            {
                Console.WriteLine("栈顶元素为:ID=" + seqStack.data[i].ID + ",Name=" + seqStack.data[i].Name + ",Age=" + seqStack.data[i].Age);
            }

            Console.Read();
        }
    }

    #region 学生数据实体
    /// <summary>
/// 学生数据实体
/// </summary>
    public class Student
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }
    }
    #endregion

    #region 栈的数据结构
    /// <summary>
/// 栈的数据结构
/// </summary>
    public class SeqStack<T>
    {
        public T[] data;

        /// <summary>
/// 栈顶指针
/// </summary>
        public int top = -1;

        public SeqStack(int lenth)
        {
            data = new T[lenth];
        }
    }
    #endregion

    public class SeqStackClass
    {
        #region 栈的初始化操作
        /// <summary>
/// 栈的初始化操作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
        public SeqStack<T> SeqStackInit<T>(int length)
        {
            SeqStack<T> seqStack = new SeqStack<T>(length);

            seqStack.top = -1;

            return seqStack;
        }
        #endregion

        #region 判断栈是否为空
        /// <summary>
/// 判断栈是否为空
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="seqStack"></param>
/// <returns></returns>
        public bool SeqStackIsEmpty<T>(SeqStack<T> seqStack)
        {
            return seqStack.top == -1;
        }
        #endregion

        #region 清空栈
        /// <summary>
/// 清空栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="seqStack"></param>
        public void SeqStackClear<T>(SeqStack<T> seqStack)
        {
            seqStack.top = -1;
        }
        #endregion

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

        #region 入栈
        /// <summary>
/// 入栈
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="seqStack"></param>
/// <param name="data"></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 name="T"></typeparam>
/// <param name="seqStack"></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 name="T"></typeparam>
/// <param name="seqStack"></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 name="T"></typeparam>
/// <param name="seqStack"></param>
/// <returns></returns>
        public int SeqStackLen<T>(SeqStack<T> seqStack)
        {
            return seqStack.top + 1;
        }
        #endregion
    }
}



Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板