04 通过写代码了解栈、顺序栈、链栈
创始人
2024-06-03 01:11:25

栈是一种运算受限的线性表,只允许在一端进行插入和删除操作​[1](https://baike.baidu.com/item/%E6%A0%88/12808149)​。这一端被称为栈顶,另一端被称为栈底​[1](https://baike.baidu.com/item/%E6%A0%88/12808149)[2](https://blog.csdn.net/m0_37961948/article/details/80245008)​。栈的特点是先进后出,类似于堆盘子​[2](https://blog.csdn.net/m0_37961948/article/details/80245008)​。栈可以用数组或链表实现

顺序栈

自定义接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _02_StackTest
{interface IStackDS{int Count { get; }int GetLength();bool IsEmpty();void Clear();void Push(T item);T Pop();T Peek();}
}

顺序栈类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _02_StackTest
{class SeqStack : IStackDS{//栈的数组结构private T[] data;//顶部索引private int top;public SeqStack(int size){data = new T[size];top = -1;}//给一个默认大小public SeqStack():this(10){}public int Count {get{return top +1;}}public void Clear(){top = -1;}public int GetLength(){return Count;}public bool IsEmpty(){return Count == 0;}public T Peek(){return data[top];}public T Pop(){T temp = data[top];top--;return temp;}public void Push(T item){data[top + 1] = item;top++;}}
}

链栈

根据网上的搜索结果​[1](http://data.biancheng.net/view/171.html)[2](https://blog.csdn.net/qq_41727218/article/details/81210027)[3](https://blog.csdn.net/m0_61395860/article/details/122814802),链栈是栈的一种实现方法,特指用链表实现栈存储结构。链栈的实现思路和顺序栈类似,顺序栈是将数组的一端做栈底,另一端做栈顶;链栈也是如此,我们通常将链表的头部做栈顶,尾部做栈底。

有元素入栈时,只需要将其插入到链表的头部;有元素出栈时,只需要从链表的头部依次摘取结点。因此,链栈实际上是一个采用头插法插入或删除数据的链表。

定义Node

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _02_StackTest
{/// /// 链栈的节点/// /// class Node{//数据private T data;//节点,用于指向下一个节点private Node next;public Node(){data = default(T);next = null;}public Node(T data ){this.data = data;next = null;}public Node(T data,Nodenext){this.data = data;this.next = next;}public Node(Node next){this.next = next;data = default(T);}public T Data { get { return data; } set { data =value; } }public Node Next { get { return next; } set { next = value; } }}
}

LinkStack(栈类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _02_StackTest
{class LinkStack : IStackDS{//栈顶元素节点private Node top;//进入栈,都是遍历,用count=0,表示栈中元素个数private int count = 0;/// /// 取得栈中元素个数/// public int Count => count;/// /// 清空栈中所有数据/// public void Clear(){count = 0;top = null;}/// /// 取得栈中元素个数/// /// public int GetLength(){return count;}/// /// 判断栈中是否数据/// /// public bool IsEmpty(){return  count == 0;}/// /// 取得栈顶中的数据,不删除栈顶/// /// public T Peek(){return top.Data;}/// /// 出栈 取得栈顶元素,然后删除/// /// public T Pop(){//获得栈顶元素T data = top.Data;//把top指向下一个节点top = top.Next;count--;return data;}/// /// 入栈/// /// public void Push(T item){//把新添加的元素作为栈顶节点(栈顶)Node newNode = new Node(item);//当前top交给下一个节点newNode.Next = top;// 新节点占领toptop = newNode;count++;}}
}

测试

using System;
using System.Collections.Generic;namespace _02_StackTest
{class Program{static void Main(string[] args){//使用链栈IStackDS stack = new LinkStack();stack.Push('a');stack.Push('b');stack.Push('c');Console.WriteLine("push a b c 之后的数据个数:" + stack.Count);char temp = stack.Pop();Console.WriteLine("Pop之后,栈顶数据为"+temp);Console.WriteLine("Pop之后,栈中数据个数为"+stack.Count);//取得栈顶数据后不删除char temp2 = stack.Peek();Console.WriteLine("Peek之后得到的数据是"+temp2);Console.WriteLine("Peek之后栈中数据的个数"+stack.Count);Console.ReadKey();}}
}

打印结果:

push a b c 之后的数据个数:3
Pop之后,栈顶数据为c
Pop之后,栈中数据个数为2
Peek之后得到的数据是b
Peek之后栈中数据的个数2

相关内容

热门资讯

猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...