zoukankan      html  css  js  c++  java
  • C#数据结构与算法系列(八):栈(Stack)

    1.介绍

    栈是一个先入后出(FILO-First In Last Out)的有序列表

    栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)

    根据栈的定义可知,最先放入栈中的元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除

    2.图解

     3.应用场景

    子程序的调用:在跳往子程序前,会先将下一个指令的地址存入堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中

    处理递归调用:和子程序的调用类似,只是出了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中

    表达式的转换【中缀表达式转后缀表达式】与求值

    二叉树的遍历

    图形的深度优先(depth-first)搜索法

    4.示例

    用数组模拟栈的使用,由于栈是一种有序列表,当然可以使用数组的结构来储存栈的数据内容,下面我们就用数组模拟栈的出栈,入栈等操作。

    示意图

     代码实现

      public class ArrayStack
        {
            private int _maxSize;
            private int[] _arr;
            private int _top = -1;
    
            public ArrayStack(int maxSize)
            {
                _maxSize = maxSize;
                _arr = new int[_maxSize];
            }
    
            public bool IsEmpty() => _top == -1;
    
            public bool IsFull() => _top == _maxSize-1;
    
            public void Push(int value)
            {
                if (IsFull())
                {
                    Console.WriteLine("栈满");
                }
                else
                {
                    _top++;
                    _arr[_top] = value;
                }
            }
    
            public int Pop()
            {
                if (IsEmpty())
                {
                    throw new Exception("栈空");
                }
                int value = _arr[_top];
    
                _top--;
    
                return value;
            }
    
            public void List()
            {
                if (IsEmpty())
                {
                    Console.WriteLine("栈空");
                }
                else
                {
                    for (int i = _top; i >= 0; i--)
                    {
                        Console.WriteLine($"stack:[{i}]={_arr[i]}");
                    }
                }
            }
    
            public static void Test()
            {
                Console.WriteLine("栈测试
    ");
    
                bool loop = true;
    
                ArrayStack stack = new ArrayStack(5);
    
                while (loop)
                {
                    Console.WriteLine("show:显示栈");
                    Console.WriteLine("exit:退出程序");
                    Console.WriteLine("push:入栈");
                    Console.WriteLine("pop:出栈");
    
                    string key = Console.ReadLine();
                    switch (key)
                    {
                        case "show":
                            stack.List();
                            break;
                        case "exit":
                            Console.WriteLine("退出程序");
                            loop = false;
                            break;
                        case "push":
                            Console.WriteLine("请输入一个数");
                            int value = int.Parse(Console.ReadLine());
                            stack.Push(value);
                            break;
                        case "pop":
                            try
                            {
                                int res=stack.Pop();
                                Console.WriteLine($"stack:{res}");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }

    效果图

  • 相关阅读:
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    【POJ
    NAT
    OSPF与ACL综合实验
  • 原文地址:https://www.cnblogs.com/vic-tory/p/13156501.html
Copyright © 2011-2022 走看看