代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。