数据结构顺序栈实现
#include<iostream>
#include<cstdlib>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return s->top==-1;
}
bool Push(SqStack *&s,ElemType e)
{
if(s->top==MaxSize-1)return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,ElemType &e)
{
if(s->top==-1)return false;
e=s->data[s->top];
return true;
}
int main()
{
SqStack *s;
ElemType e;
InitStack(s);
Push(s,1);
Push(s,100);
Pop(s,e);
cout<<e<<endl;
GetTop(s,e);
cout<<e<<endl;
DestroyStack(s);
return 0;
}