这是编码
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define MAXSIZE 100
#define OVERFLOW 0
#define ERROR 0
#define ok 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *base;
//栈底指针
SElemType *top;
//栈顶指针
int stacksize;
}SqStack;
/****初始化****/
Status InitStack(SqStack &S)
{
S.base=new SElemType[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=MAXSIZE;
return ok;
}
/****清空栈*****/
Status ClearStack(SqStack &S)
{
S.top = S.base;
return ok;
}
/*****判断栈是否为空栈******/
Status StackEmpty(SqStack S)
{
if (S.top == S.base)
return ERROR;
else
return TRUE;
}
/****取栈顶元素****/
Status GetTop(SqStack &S)
{
if (StackEmpty(S))
{
cout << "栈为空!" << endl;
return 0;
}
return *(S.top - 1);
}
/******入栈*****/
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base==S.stacksize) return ERROR; //栈满
*S.top++=e;
return ok;
}
/*****栈的长度******/
Status StackLength(SqStack S)
{
if (S.top == S.base)
return FALSE;
else
return (S.top - S.base);//也可以直接返回S.top - S.base
}
/*****遍历元素*****/
void StackTraverse(SqStack S)
{
SElemType *p=S.base;
while (p<S.top)
{cout<<*p;p++;}
}
/*****出栈******/
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
//栈空
e=*--S.top;
return ok;
}
int main()
{
SqStack S;
cout << "构造了一个空栈" << endl;;
InitStack(S);
int i,n,e;
cout<<"输入栈的长度;"<<endl;
cin >> n;
Push(S,e);
for(i=1;i<=n;i++)
{
cout << "输入栈的第"<<i<<"个元素"<<endl;
++*S.top;
cin >>e;
}
cout << "……求出栈的长度……" <<endl;
int m;
m=StackLength(S);
cout<< "栈的长度是:" <<endl;
cout<< m << endl;
cout<< "遍历输出栈中的所有元素:" <<endl;
StackTraverse(S);
cout<< endl;
cout<< "……输出栈顶元素……" <<endl;
e = GetTop(S);
cout<< "栈顶元素是:" <<endl;
cout<< e <<endl;
return 0;
}