当前位置:网站首页>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();
}
边栏推荐
- 535. encryption and decryption of tinyurl: design a URL simplification system
- qt json
- AES-128-CBC-Pkcs7Padding加密PHP实例
- Pygame 对图像进行翻转
- How to create new user for ORACLE 19c (CDB & PDB)
- Gbase 8s extended external connection 1
- Gbase8s database select has order by Clause 5
- MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
- Recommended model reproduction (II): fine arrangement model deepfm, DIN
- Comment calculer Win / Tai / Loss in paired t - test
猜你喜欢

C#通過中序遍曆對二叉樹進行線索化

Go learning - build a development environment vscode development environment golang

Adjacency matrix and adjacency table structure of C # realization graph

LR、CR纽扣电池对照表
![[cloud native] 2.4 kubernetes core practice (middle)](/img/1e/b1b22caa03d499387e1a47a5f86f25.png)
[cloud native] 2.4 kubernetes core practice (middle)

How to create new user for ORACLE 19c (CDB & PDB)

【云原生】2.4 Kubernetes 核心实战(中)

从Mpx资源构建优化看splitChunks代码分割

Uncover the practice of Baidu intelligent test in the field of automatic test execution

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture
随机推荐
Syntax of gbase8s database incompatible with for update clause
oracle 19c : change the user sys/system username pasword under Linux
Adjacency matrix and adjacency table structure of C # realization graph
模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!
MIT linear algebra Chinese Notes
[环境配置]PWC-Net
面试突击61:说一下MySQL事务隔离级别?
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
huffman编码
Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released
LeetCode_双指针_中等_328.奇偶链表
C # indexe l'arbre binaire en traversant l'ordre moyen
Gbase8s database select has order by Clause 6
Qt中的UI文件介绍
[JUC series] ThreadLocal of synchronization tool class
推荐模型复现(三):召回模型YoutubeDNN、DSSM
Proteus软件初学笔记
1. opencv realizes simple color recognition
How to fix ORA-01017:用户名/口令无效 登录拒绝
C#实现二叉树的层次遍历