C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
来源:互联网 发布:mac air 安装windows 编辑:程序博客网 时间:2024/05/03 06:22
//顺序栈的基本操作
#include<stdio.h>
#define MaxSize 50
typedef int ElemType;
//定义栈结构体
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack &S)
{
S.top = -1;
}
//判断栈是否为空
bool StackEmpty(SqStack S)
{
if(S.top == -1)
return true; //栈为空
else
return false;
}
//入栈
bool Push(SqStack &S,ElemType x)
{
if(S.top == MaxSize-1)
return false;
S.data[++S.top] = x;
return true;
}
//出栈
bool Pop(SqStack &S,ElemType x)
{
if(S.top == -1)
return false;
x = S.data[S.top--];
printf("%d\n",x);//为查看栈的出栈元素
return true;
}
//获取栈顶元素
bool GetTop(SqStack S,ElemType &x)
{
if(S.top == -1)
return false;
x = S.data[S.top];
printf("%d\n",x);//为查看栈的栈顶元素
return true;
}
void main()
{
SqStack s;
int m,x;
InitStack(s);
Push(s,3);
Push(s,9);
Push(s,17);
Pop(s,x);
m = StackEmpty(s);
GetTop(s,x);
printf("%d\n",m);
#include<stdio.h>
#define MaxSize 50
typedef int ElemType;
//定义栈结构体
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack &S)
{
S.top = -1;
}
//判断栈是否为空
bool StackEmpty(SqStack S)
{
if(S.top == -1)
return true; //栈为空
else
return false;
}
//入栈
bool Push(SqStack &S,ElemType x)
{
if(S.top == MaxSize-1)
return false;
S.data[++S.top] = x;
return true;
}
//出栈
bool Pop(SqStack &S,ElemType x)
{
if(S.top == -1)
return false;
x = S.data[S.top--];
printf("%d\n",x);//为查看栈的出栈元素
return true;
}
//获取栈顶元素
bool GetTop(SqStack S,ElemType &x)
{
if(S.top == -1)
return false;
x = S.data[S.top];
printf("%d\n",x);//为查看栈的栈顶元素
return true;
}
void main()
{
SqStack s;
int m,x;
InitStack(s);
Push(s,3);
Push(s,9);
Push(s,17);
Pop(s,x);
m = StackEmpty(s);
GetTop(s,x);
printf("%d\n",m);
}
程序结果:
0 0
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 链栈:初始化、判断栈空、入栈、出栈、获取栈顶元素等
- 顺序栈的基本操作实现---入栈、出栈、判断是否为空
- 数据结构栈的数组实现初始化,入栈,出栈,获取栈顶元素,栈的长度等操作
- C++栈的初始化,入栈,出栈,获取栈顶元素等操作
- [数据结构]c语言实现顺序栈的入栈,出栈,清空,销毁等操作
- stack容器的用法:入栈、出栈、访问栈顶元素,判断是否为空
- 栈的基本实现(入栈,出栈,清空,判断是否为空)
- 数据机构(五) 顺序栈的基本操作 --- 包含初始化,入栈和出栈
- JAVA顺序栈操作——初始化、入栈、出栈、判断空栈、判断栈满
- [数据结构]c语言实现链栈的入栈,出栈,清空,销毁等操作
- 栈的基本操作模板类,包括栈的压入,出栈,删除栈顶元素,清空栈等操作
- C语言实现循环队列基本操作(初始化、判断队空、入队、出队)
- 判断元素出栈,入栈顺序的合法性
- 判断元素入栈,出栈顺序的合法性
- 判断元素出栈、入栈顺序的合法性
- 判断元素出栈,入栈顺序的合法性
- 判断元素出栈、入栈顺序的合法性
- Android通讯录获取联系方式
- GBK编码详细解析(附GBK码位分布图)
- Android Studio如何导入jar包
- C#自动更新
- iOS 本地推送 UILocalNotification
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 基于注解的SpringMVC简单介绍
- haproxy 页面重定向(域名跳转)
- XSD (xml Schema Definition)
- LeetCode之Simplify Path
- 通过intent action指定类型操作(打开文本,网页,pdf,word,分享...)过滤处理
- iOS-ui-UISlider
- JAVA & Eclipse 笔记
- hive编程指南--employees表数据定义