以下代码对顺序栈的建立的两种方式进行了实现:一、结构体 二、直接用数组进行操作
#include<iostream>
#define maxSize 50
using namespace std;
typedef struct
{
int data[maxSize];
int top;
}SqStack;
void initStack(SqStack &st)
{
st.top=-1;
}
int isEmpty(SqStack st)
{
if(st.top==-1)
return 1;
else return 0;
}
int Push(SqStack &st,int x)
{
if(st.top==maxSize-1)
return 0;
++(st.top);
st.data[st.top]=x;
return 1;
}
int Pop(SqStack &st,int &x)
{
if(st.top==-1)
return 0;
x=st.data[st.top];
--(st.top);
return 1;
}
int main()
{
/*以上为对栈的建立,判断是否为空,进栈出栈操作,比较麻烦,还有一种简单写法*/
/*声明一个栈,且初始化*/
int a[maxSize]; int top=-1;
int x=0;
/*进栈操作*/
a[++top]=x;
/*出栈操作*/
x=a[top--];
return 0;
}
posted on 2012-08-22 07:21
yyj 阅读(382)
评论(0) 编辑 收藏 引用