最近在学习算法基础,本篇文章作为一个记录,也算是一次实践和总结。(顺便也深入C#运行时学习一下)
目录
1. 栈是什么
栈是一种特殊的线性表(数据逻辑结构中的一种,定义是其组成元素之间具有线性关系的一种线性结构),其插入和删除操作只能在一端进行。
1 个人理解: 2 是一种最基本的数据结构,数据结构方面必须夯实的基础知识
特征(也可以说是应用场景):
后进先出(队列则为先进先出)
2. Stack 自定义实现
Github: https://github.com/HadesKing/DataStructureAndAlgorithms
代码路径:BasicDataStructure\src\LD.BasicDataStructures\Stacks
Stack实现方式有两种:顺序存储和链式存储(顺序栈和链式栈)
为了便于使用,我对栈进行了抽象,形成了一个接口IStack
1 // Copyright (c) 刘迪. All rights reserved. 2 // 3 // Author: 刘迪 4 // Create Time: 2021-04-30 5 // 6 // Modifier: xxx 7 // Modifier time: xxx 8 // Description: xxx 9 // 10 // Modifier: xxx 11 // Modifier time: xxx 12 // Description: xxx 13 // 14 // 15 // 16 17 using System; 18 19 namespace LD.BasicDataStructures.Stacks 20 { 21 /// <summary> 22 /// 栈的接口 23 /// </summary> 24 /// <typeparam name="T">type of data element</typeparam> 25 /// <remarks> 26 /// 主要用于定义栈的公共操作 27 /// </remarks> 28 public interface IStack<T> 29 { 30 /// <summary> 31 /// Gets the number of elements contained in the Stack. 32 /// </summary> 33 Int32 Count { get; } 34 35 /// <summary> 36 /// Verify that it is empty. 37 /// </summary> 38 /// <returns> 39 /// <c>true</c> Is empty. 40 /// <c>false</c> Not is empty. 41 /// </returns> 42 bool IsEmpty(); 43 44 /// <summary> 45 /// Add a data element to the stack. 46 /// </summary> 47 /// <param name="element">value of data element</param> 48 /// <returns> 49 /// <c>true</c> The operation was successful. 50 /// <c>false</c> The operation failed. 51 /// </returns> 52 bool Push(T element); 53 54 /// <summary> 55 /// Pop up a data element from the stack.If there is no data element in the stack, it returns null. 56 /// </summary> 57 /// <returns> 58 /// Data element being popped.If there is no data element in the stack, it returns null. 59 /// </returns> 60 T Pop(); 61 62 /// <summary> 63 /// Get a data element from the stack.If the stack is empty, return null. 64 /// </summary> 65 /// <returns> 66 /// Data element.If the stack is empty, return null 67 /// </returns> 68 T Get(); 69 70 } 71 }