zoukankan      html  css  js  c++  java
  • 数据结构与算法绪论学习 Day4_栈

    1  栈的概念和操作

    1.1 栈的基本特征

      栈限定只在表尾进行插入和删除的操作,也就是传统意义上的”先进后出“,所以栈也是线性表的一种

    1.2 栈的基本操作

    •   插入操作 即入栈
    •   删除操作 即出栈

    2  栈的基本实现

      栈属于线性表中的一种,那么就有顺序表和链表两种实现方式,但是不管哪种,都需要下面几个方法

    Stack<T>()

    创建一个空的栈

    void Push(T s)

    往栈中添加一个新的元素

    T Pop()

    移除并返回最近添加的元素

    bool IsEmpty()

    栈是否为空

    int Size()

    栈中元素的个数

    2.1 栈的顺序存储实现

    using System;
    
    namespace 数据结构.线性表.栈
    {
        public class StackArr<T>
        {
            private T[] Nodes;
            private int index;
            /// <summary>
            /// 初始化栈
            /// </summary>
            /// <param name="length"></param>
            public StackArr (int length)
            {
                Nodes = new T[length];
                this.index = 0;
            }
            /// <summary>
            /// 入栈操作
            /// </summary>
            /// <param name="node"></param>
            public void Push (T node)
            {
                if (index == Nodes.Length)
                {
                    ResizeLength(Nodes.Length * 2);
                }
                Nodes[index] = node;
                index++;
                Console.WriteLine($"栈的长度是:{Size()},表的长度是{this.Nodes.Length}");
            }
            /// <summary>
            /// 出栈操作
            /// </summary>
            /// <returns></returns>
            public T Pop ()
            {
                if (index == 0)
                {
                    return default;
                }
                T node = Nodes[index - 1];
                index--;
                Nodes[index] = default(T);
                if (index > 0 && index == Nodes.Length / 4)
                {
                    ResizeLength(Nodes.Length / 2);
                }
                Console.WriteLine($"取出的元素是{node};栈的长度是:{Size()},表的长度是{this.Nodes.Length}");
                return node;
            }
            /// <summary>
            // 重构栈
            /// </summary>
            /// <param name="newLength"></param>
            public void ResizeLength (int newLength)
            {
                T[] newNodes = new T[newLength];
                if (newLength > Nodes.Length)
                {
                    for (var i = 0; i < Nodes.Length; i++)
                    {
                        newNodes[i] = Nodes[i];
                    }
                }
                else
                {
                    for (var i = 0; i < newLength; i++)
                    {
                        newNodes[i] = Nodes[i];
                    }
                }
                Nodes = newNodes;
            }
            /// <summary>
            /// 获取栈的长度
            /// </summary>
            /// <returns></returns>
            public int Size ()
            {
                return this.index;
            }
            /// <summary>
            /// 判断
            /// </summary>
            /// <returns></returns>
            public bool IsEmpty () {
                return index == 0;
            }
        }
    }

    2.2 栈的链式存储实现

    using System;
    using 数据结构.链表;
    
    namespace 数据结构.线性表.栈
    {
        public class StackALinkList<T>
        {
            int index;
            /// <summary>
            /// 头结点
            /// </summary>
            private Node<T> head;
    
            /// <summary>
            /// 初始化栈
            /// </summary>
            public StackALinkList ()
            {
                head = null;
            }
            /// <summary>
            /// 入栈操作
            /// </summary>
            /// <param name="node"></param>
            public void Push (T node)
            {
                Node<T> newNode = new Node<T>(node);
                newNode.Next = this.head;
                this.head = newNode;
                index++;
                Console.WriteLine($"入栈的元素是{node};栈的长度是:{Size()}");
            }
            /// <summary>
            /// 出栈操作
            /// </summary>
            /// <returns></returns>
            public T Pop ()
            {
                if (IsEmpty())
                    return default;
                var node = head;
                head = head.Next;
                index--;
                Console.WriteLine($"出栈的元素是{node.Item};栈的长度是:{Size()}");
                return node.Item;
            }
            /// <summary>
            /// 获取栈的长度
            /// </summary>
            /// <returns></returns>
            public int Size ()
            {
                return this.index;
            }
            /// <summary>
            /// 判断
            /// </summary>
            /// <returns></returns>
            public bool IsEmpty () {
                return index == 0;
            }
        }
    }

    3  C#中栈的实现

    c#中也有栈的实现,即 命名空间System.Collections.Generic内的Stack,也提供了Push,Pop方法,同时还提供了Peek方法(获取头部的元素但不删除它),没有反看源码,但是查资料C#内部是用的数组的形式来实现栈的。

    以后遍历元素,使用栈貌似会方便快捷很多啊

  • 相关阅读:
    UNIX常用shell
    exit函数
    linux消息队列
    互斥量
    RCS版本控制
    linux samba
    UML建模
    linux syslog
    python基础-列表List及内置方法
    仿美团详情页与购物车源码-详情页
  • 原文地址:https://www.cnblogs.com/yuchenghao/p/13069859.html
Copyright © 2011-2022 走看看