当前位置:网站首页>C#实现堆栈结构的定义、入栈、出栈
C#实现堆栈结构的定义、入栈、出栈
2022-06-29 12:05:00 【黄昏和星空】
本文介绍C#实现堆栈结构的定义、入栈、出栈
1、栈的定义
///
/// 栈定义
///
public struct SqStack {
public int[] data;
public int top;//栈顶
}
2、判断栈是否为空
///
/// 判断栈是否为空
///
///
///
bool StackEmpty(SqStack S) {
if (S.top == -1)
{
return true;
}
else {
return false;
}
}
3、栈初始化
///
/// 栈初始化
///
///
static void InitStack(ref SqStack S)
{
S.top = -1;
}
4、压栈
///
/// 压栈
///
///
///
static bool Push(ref SqStack S,int e) {
if (S.top >= MaxSize - 1) {
return false;
}
S.top = S.top+1;
S.data[S.top] = e;//先加1,再进栈
return true;
}
5、出栈
///
/// 出栈
///
///
///
///
static bool PoP(ref SqStack S, ref int e) {
if (S.top==-1) { return false; }
e = S.data[S.top–];//出栈
return true;
}
6、读取栈顶元素
///
///
///读取栈顶元素
///
///
///
///
bool GetTop(ref SqStack S, ref int e) {
if (S.top == -1) { return false; }
e = S.data[S.top];//读取元素
return true;
}
7、实际main函数测试
public const int MaxSize = 50;//定义栈的元素最大值
static void Main(string[] args)
{
SqStack S = new SqStack() ;
S.data = new int[MaxSize];
int x=0;
//初始化栈
InitStack(ref S);
//进栈
while (x!=999) {
Console.WriteLine(“请输入栈元素”);
x = int.Parse(Console.ReadLine());//字符转为整型
if (x != 999) {
Push(ref S, x);
}
}
Console.WriteLine("栈顶元素:"+S.data[S.top]+"栈的元素个数:"+(S.top+1));
Console.WriteLine("出栈元素如下");
while (S.top!=-1) {
PoP(ref S,ref x);
Console.WriteLine(x);
}
Console.ReadLine();
}
边栏推荐
- Interview shock 61: tell me about MySQL transaction isolation level?
- How to fix ORA-01017:用户名/口令无效 登录拒绝
- 超 Nice 的表格响应式布局小技巧
- File contained log poisoning (user agent)
- 推荐模型复现(三):召回模型YoutubeDNN、DSSM
- 从Mpx资源构建优化看splitChunks代码分割
- 【君正T31】只读rootfs文件系统squashfs的解压和打包
- QQ集体被盗号,猝不及防的大型社死名场面
- Set operator of gbase8s database in combined query
- JVM之方法区
猜你喜欢

Proteus Software beginner notes

推荐模型复现(一):熟悉Torch-RecHub框架与使用

Principle and process of MySQL master-slave replication

go 学习-搭建开发环境vscode开发环境golang

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture

Interview shock 61: tell me about MySQL transaction isolation level?

AGCO AI frontier promotion (6.29)

LeetCode_ Double pointer_ Medium_ 328. parity linked list

Comment calculer Win / Tai / Loss in paired t - test

Can software related inventions be patented in India?
随机推荐
Testing -- automated testing: about the unittest framework
【JUC系列】同步工具类之ThreadLocal
LeetCode_双指针_中等_328.奇偶链表
Recommended model reproduction (II): fine arrangement model deepfm, DIN
Recurrence of recommended models (III): recall models youtubednn and DSSM
nacos启动报错
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture
Interpolated scatter data
Yunlong fire version aircraft battle (full version)
Qt的信号与槽
Golang image/png processing image rotation writing
C # indexe l'arbre binaire en traversant l'ordre moyen
Interview shock 61: tell me about MySQL transaction isolation level?
Is it safe for Orient Fortune Securities to open an account? Handling of securities account opening
Gbase8s database select has order by Clause 1
Qt中的UI文件介绍
《Go题库·14》WaitGroup的坑
Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication
[leetcode] 14. Longest public prefix
C#实现二叉排序树定义、插入、构造