C#实现顺序栈和链栈的代码实例
作者:Czhenya 时间:2021-08-17 02:36:40
自己定义的栈的接口,完全是按照栈的常用方法以及命名方式实现:
注意以下类,接口都是在一个命名空间下
栈的接口:包括了常用的方法
namespace 栈
{
interface IStackDS<T>
{
int Count { get; }
int GetLength();
bool IsEmpty();
void Clear();
void Push(T item);
T Pop();
T Peek();
}
}
顺序栈的实现,参照顺序表实现
namespace 栈
{
class SeqStack<T> : IStackDS<T>
{
private T[] data; //栈
private int top; //栈顶
//构造函数
public SeqStack(int size) {
data = new T[size];
top = -1;
}
public SeqStack() : this(10)
{
data = new T[10];
top = -1;
}
//栈的元素个数属性
public int Count
{
get
{
return top + 1;
}
}
//清空栈的方法
public void Clear()
{
top = -1;
}
//获取栈的长度的方法
public int GetLength()
{
return Count;
}
//查看栈顶元素是什么
public T Peek()
{
if (top == -1)
{
Console.WriteLine("当前栈没有元素");
return default(T);
}
return data[top];
}
//出栈(取出栈顶元素,并移除)
public T Pop()
{
T temp = data[top];
top--;
return temp;
}
//入栈
public void Push(T item)
{
data[top + 1] = item;
top++;
}
//栈中是否源元素
public bool IsEmpty()
{
return Count==0;
}
}
}
以上是顺序栈的实现
下面是链栈的实现,定义一个节点类
namespace 栈
{
/// <summary>
/// 链栈的节点
/// </summary>
/// <typeparam name="T"></typeparam>
class Node<T>
{
private T data; //数据
private Node<T> next; //指向下一个的引用
//四个构造函数,不是都有用,,以及上面两个变量的属性,
public Node()
{
data = default(T);
next = null;
}
public Node(T data)
{
this.data = data;
next = null;
}
public Node(T data , Node<T> index)
{
this.data = data;
this.next = index;
}
public Node(Node<T> index)
{
data = default(T);
next = index;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
链栈的实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 栈
{
class LinkStark<T> : IStackDS<T>
{
private Node<T> top; //栈顶元素节点
private int count = 0; //栈里面的元素
public int Count
{
get
{
return count;
}
}
/// <summary>
/// 清空栈中数据
/// </summary>
public void Clear()
{
count = 0;
top = null;
}
/// <summary>
/// 栈的长度
/// </summary>
/// <returns></returns>
public int GetLength()
{
return Count;
}
/// <summary>
/// 栈中是否有数据
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return count == 0;
}
public T Peek()
{
return top.Data;
}
/// <summary>
/// 出栈 取得栈顶元素然后删除
/// </summary>
/// <returns></returns>
public T Pop()
{
T data = top.Data;
top = top.Next;
count--;
return data;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="item"></param>
public void Push(T item)
{//把新添加的元素作为头结点,就是栈顶
Node<T> newNode = new Node<T>(item);
newNode.Next = top;
top = newNode;
count++;
}
}
}
以上是链栈的实现
下面是测试:
C#库中带的栈
自己的写的顺序栈
自己写的链栈
amespace 栈
{
class Program
{
static void Main(string[] args)
{
//使用BCL中的栈
//Stack<char> stack = new Stack<char>();
//使用自己的栈
// SeqStack<char> stack = new SeqStack<char>();
//使用自己的链栈
IStackDS<char> stack = new LinkStark<char>();
stack.Push('a');
stack.Push('b');
stack.Push('c');
Console.WriteLine("push后的数据个数"+ 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();
}
}
}
下篇队列的实现同理:https://www.jb51.net/article/149555.htm
来源:https://blog.csdn.net/czhenya/article/details/78077821
标签:c#,栈
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp.net页面中如何获取Excel表的内容
2022-11-18 11:40:31
SpringAOP 如何通过JoinPoint获取参数名和值
2023-11-01 00:50:36
![](https://img.aspxhome.com/file/2023/1/69351_0s.png)
深入理解Java中的接口
2023-11-08 23:52:43
Android7.0 工具类:DiffUtil详解
2023-02-24 01:43:46
![](https://img.aspxhome.com/file/2023/6/88086_0s.gif)
java中File类的使用方法
2023-09-11 11:49:55
Java LinkedHashMap深入分析源码
2022-07-31 03:05:43
![](https://img.aspxhome.com/file/2023/2/62092_0s.png)
解决MyBatis @param注解参数类型错误异常的问题
2023-12-01 06:41:45
SpringCloud2020整合Nacos-Bootstrap配置不生效的解决
2023-11-29 02:42:47
Kotlin封装RecyclerView Adapter实例教程
2023-11-06 01:54:23
Kotlin与Java的区别详解
2023-06-22 16:25:58
![](https://img.aspxhome.com/file/2023/6/75696_0s.jpg)
android截屏功能实现代码
2022-02-02 19:45:00
![](https://img.aspxhome.com/file/2023/3/87533_0s.jpg)
java文件操作之java写文件简单示例
2023-10-21 14:37:13
Java中的复合数据类型
2022-10-05 08:08:08
![](https://img.aspxhome.com/file/2023/3/69273_0s.png)
详解Spring Cloud Gateway修改请求和响应body的内容
2022-12-03 04:40:05
![](https://img.aspxhome.com/file/2023/1/91051_0s.png)
在spring中手写全局异常拦 截器
2023-11-09 01:25:42
Java源码解析HashMap的keySet()方法
2023-11-11 06:33:05
![](https://img.aspxhome.com/file/2023/7/58837_0s.png)
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
![](https://img.aspxhome.com/file/2023/9/82419_0s.gif)
SpringBoot的异常处理流程是什么样的?
2021-07-09 17:54:40
![](https://img.aspxhome.com/file/2023/4/81894_0s.png)
解析spring boot与ireport 整合问题
2021-12-15 17:49:25
Java通过HttpClient进行HTTP请求的代码详解
2022-06-04 09:50:20