C#数据结构之堆栈(Stack)实例详解
作者:Jimmy.Yang 时间:2022-12-17 21:25:37
本文实例讲述了C#数据结构之堆栈(Stack)。分享给大家供大家参考,具体如下:
堆栈(Stack)最明显的特征就是“先进后出”,本质上讲堆栈也是一种线性结构,符合线性结构的基本特点:即每个节点有且只有一个前驱节点和一个后续节点。
相对前面学习过的顺序表、链表不同的地方在于:Stack把所有操作限制在"只能在线性结构的某一端"进行,而不能在中间插入或删除元素。下面是示意图:
从示意图中可以看出,堆栈有二种实现方式:基于数组的顺序堆栈实现、类似链表的链式堆栈实现
先抽象堆栈的接口IStack:
namespace 栈与队列
{
public interface IStack<T>
{
/// <summary>
/// 返回堆栈的实际元素个数
/// </summary>
/// <returns></returns>
int Count();
/// <summary>
/// 判断堆栈是否为空
/// </summary>
/// <returns></returns>
bool IsEmpty();
/// <summary>
/// 清空堆栈里的元素
/// </summary>
void Clear();
/// <summary>
/// 入栈:将元素压入堆栈中
/// </summary>
/// <param name="item"></param>
void Push(T item);
/// <summary>
/// 出栈:从堆栈顶取一个元素,并从堆栈中删除
/// </summary>
/// <returns></returns>
T Pop();
/// <summary>
/// 取堆栈顶部的元素(但不删除)
/// </summary>
/// <returns></returns>
T Peek();
}
}
顺序堆栈(SeqStack)的实现:
using System;
using System.Text;
namespace 栈与队列
{
public class SeqStack<T>:IStack<T>
{
private int maxsize;
private T[] data;
private int top;
public SeqStack(int size)
{
data = new T[size];
maxsize = size;
top = -1;
}
#region //接口实现部分
public int Count()
{
return top + 1;
}
public void Clear()
{
top = -1;
}
public bool IsEmpty()
{
return top == -1;
}
public void Push(T item)
{
if (IsFull())
{
Console.WriteLine("Stack is full");
return;
}
data[++top] = item;
}
public T Pop()
{
T tmp = default(T);
if (IsEmpty())
{
Console.WriteLine("Stack is empty");
return tmp;
}
tmp = data[top];
top--;
return tmp;
}
public T Peek()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}
return data[top];
}
#endregion
public bool IsFull()
{
return top == maxsize - 1;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = top;i>=0;i--)
{
sb.Append(data[i] + ",");
}
return sb.ToString().Trim(',');
}
}
}
链式堆栈(LinkStack)的实现
先定义节点Node.cs
namespace 栈与队列
{
public class Node<T>
{
private T data;
private Node<T> next;
public Node(T data, Node<T> next)
{
this.data = data;
this.next = next;
}
public Node(Node<T> next)
{
this.next = next;
this.data = default(T);
}
public Node(T data)
{
this.data = data;
this.next = null;
}
public Node()
{
this.data = default(T);
this.next = null;
}
public T Data {
get { return this.data; }
set { this.data = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
}
下面是LinkStack.cs
using System;
using System.Text;
namespace 栈与队列
{
public class LinkStack<T>:IStack<T>
{
private Node<T> top;
private int num;//节点个数
/// <summary>
/// 顶部节点
/// </summary>
public Node<T> Top
{
get { return top; }
set { top = value; }
}
public LinkStack()
{
top = null;
num = 0;
}
public int Count()
{
return num;
}
public void Clear()
{
top = null;
num = 0;
}
public bool IsEmpty()
{
if (top == null && num == 0)
{
return true;
}
else
{
return false;
}
}
public void Push(T item)
{
Node<T> q = new Node<T>(item);
if (top == null)
{
top = q;
}
else
{
q.Next = top;
top = q;
}
num++;
}
public T Pop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}
Node<T> p = top;
top = top.Next;
num--;
return p.Data;
}
public T Peek()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}
return top.Data;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (top != null)
{
sb.Append(top.Data.ToString() + ",");
Node<T> p = top;
while (p.Next != null)
{
sb.Append(p.Next.Data.ToString()+ ",");
p = p.Next;
}
}
return sb.ToString();
}
}
}
测试代码片段:
Console.WriteLine("顺序堆栈测试开始...");
SeqStack<int> seqStack = new SeqStack<int>(10);
seqStack.Push(1);
seqStack.Push(2);
seqStack.Push(3);
Console.WriteLine(seqStack);
Console.WriteLine(seqStack.Peek());
Console.WriteLine(seqStack);
Console.WriteLine(seqStack.Pop());
Console.WriteLine(seqStack);
Console.WriteLine("链堆栈测试开始...");
LinkStack<int> linkStack = new LinkStack<int>();
linkStack.Push(1);
linkStack.Push(2);
linkStack.Push(3);
Console.WriteLine(linkStack);
Console.WriteLine(linkStack.Peek());
Console.WriteLine(linkStack);
Console.WriteLine(linkStack.Pop());
Console.WriteLine(linkStack);
Console.ReadLine();
注: .Net中System.Collections.Generic.Stack<T>已经提供了堆栈的基本实现,明白原理后,仍然推荐大家使用内置的实现。
希望本文所述对大家C#程序设计有所帮助。
标签:C#,数据结构
0
投稿
猜你喜欢
java生成jar包并且单进程运行的实例
2021-08-30 00:40:55
详解Java编程中JavaMail API的使用
2022-08-02 06:18:23
C# TrieTree介绍及实现方法
2022-02-10 22:04:53
DataGridView实现点击列头升序和降序排序
2022-07-31 21:54:01
Java 线程的优先级(setPriority)案例详解
2023-11-12 23:46:39
socket编程时的发送与接收数据时的问题解析
2022-12-31 03:57:28
Android实现自动提取短信验证码功能
2022-11-19 23:58:49
C#写入XML文档
2022-03-21 18:08:33
解析ScrollView--仿QQ空间标题栏渐变
2021-06-20 22:44:54
C#判断上传文件是否是图片以防止木马上传的方法
2023-10-02 13:13:50
android使用PullToRefresh实现下拉刷新和上拉加载
2023-08-06 11:06:58
SpringCloud全面解析@FeignClient标识接口的过程
2023-08-05 12:34:44
基于String和List<String>间的相互转换方式
2022-09-25 15:52:21
WinForm判断关闭事件来源于用户点击右上角“关闭”按钮的方法
2021-05-28 13:00:43
java基础的详细了解第六天
2021-11-05 16:18:49
Java C++题解leetcode 1684统计一致字符串的数目示例
2023-04-23 09:06:31
Java并发问题之乐观锁与悲观锁
2022-07-13 19:59:05
C#实现学生成绩管理系统
2021-06-05 22:51:31
详解Android 进程
2023-04-26 07:37:55
Android实现手势滑动识别功能
2022-06-23 00:16:59