当前位置:网站首页>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();
}
边栏推荐
- 高校如何基于云原生构建面向未来的智慧校园?全栈云原生架构VS传统IT架构
- 1. opencv realizes simple color recognition
- 2022.6.28-----leetcode.324
- [leetcode] 14. Longest public prefix
- Gbase8s database into table clause
- 推荐模型复现(二):精排模型DeepFM、DIN
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
- 地球观测卫星数据
- Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication
- Gbase8s database for update clause
猜你喜欢

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

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

如何计算win/tai/loss in paired t-test

Method area of JVM

推荐模型复现(三):召回模型YoutubeDNN、DSSM
![[comprehensive case] credit card virtual transaction identification](/img/85/9915ab9a6100a022dfa1c0050d554f.png)
[comprehensive case] credit card virtual transaction identification

After class assignment of module 5 of the construction practice camp

Principle and process of MySQL master-slave replication

Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released

Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!
随机推荐
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
Is it safe for Orient Fortune Securities to open an account? Handling of securities account opening
Newton inequality
1. opencv realizes simple color recognition
23、 1-bit data storage (delay line / core /dram/sram/ tape / disk / optical disc /flash SSD)
【LeetCode】14、最长公共前缀
Go learning - build a development environment vscode development environment golang
Unexpected ‘debugger‘ statement no-debugger
Interview shock 61: tell me about MySQL transaction isolation level?
Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication
C#实现二叉树非递归中序遍历程序
ERP Kingdee for preparing BOM
Recurrence of recommended models (III): recall models youtubednn and DSSM
[cloud native] 2.4 kubernetes core practice (middle)
qt 自定义控件 :取值范围
推荐模型复现(一):熟悉Torch-RecHub框架与使用
多项目开发入门-业务场景关联基础入门测试 工资表
How to install oracle19c in Centos8
[leetcode] 14. Longest public prefix
倍福TwinCAT配置、调试第三方伺服详细讲解--以汇川IS620N为例子