1 Star 0 Fork 0

富祥还晚钟/DataStructure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SqStack 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
Feng Zhang 提交于 2019-10-05 23:15 +08:00 . Create SqStack
#include<iostream>
#include<cstdlib>
using namespace std;
typedef int ElementType;
#define MaxSize 20
typedef struct
{
ElementType data[MaxSize];
int top;
}SqStack;
/*初始化栈*/
void InitialStack(SqStack &st)
{
st.top = -1;
}
/*判断栈是否为空*/
int StackEmpty(SqStack &st)
{
return (st.top == -1);
}
/*进栈算法*/
void Push(SqStack &st, ElementType x)
{
if (st.top == MaxSize - 1)
{
cout << "栈满" << endl;
return;
}
st.data[++st.top] = x;
}
/*出栈算法*/
void Pop(SqStack &st, ElementType &x)
{
if (st.top == -1)
{
cout << "栈空" << endl;
return;
}
x = st.data[st.top--];
return;
}
/*取栈顶元素*/
void GetTop(SqStack &st, ElementType &x)
{
if (st.top == -1)
{
cout << "栈空" << endl;
return;
}
x = st.data[st.top];
return;
}
int main()
{
int Element[10] = { 2,10,1,45,47,11,12,54,69,66 };
SqStack stack;
ElementType element,x;
InitialStack(stack);
for (int i = 0; i < 10; i++)
{
cout << "Push:" << Element[i]<<" ";
Push(stack, Element[i]);
GetTop(stack, element);
cout << "Top:" <<element<< endl;
}
for (int i = 0; i < 5; i++)
{
GetTop(stack, x);
cout << "Top:" << x <<" ";
Pop(stack, element);
cout << "Pop:" << element << endl;
}
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xkr-osChina/DataStructure.git
git@gitee.com:xkr-osChina/DataStructure.git
xkr-osChina
DataStructure
DataStructure
master

搜索帮助