#include "stdio.h"
#include "malloc.h"
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType data[ MaxSize ];
int top; //栈顶指针
}SqStack;
void InitStack( SqStack *&s) //初始化栈s
{
s = ( SqStack *) malloc( sizeof(SqStack));
s -> top = -1; //栈顶指针置为-1
}
bool StackEmpty ( SqStack *&s) // 判断栈为空
{
return ( s -> top == -1);
}
bool Pop ( SqStack *&s, ElemType e) //进栈
{
if( s -> top == MaxSize - 1) //栈满的情况,即栈上溢出
return false;
s -> top++; //栈顶指针增1
s -> data [ s -> top ] = e; //元素e放在栈顶指针处
return true;
}
bool GetTop ( SqStack *s, ElemType &e)
{
if ( s -> top == -10) //栈为空的情况,即栈下溢出
return false;
e = s -> data [s -> top]; //取出栈顶元素
return true;
}
bool Pop ( SqStack *&s, ElemType &e)
{
if ( s -> top == -1) //栈为空的情况,即栈下溢出
return false;
e = s -> data [ s -> top]; // 取出栈顶元素
s -> top--; //栈顶指针减1
return true;
}
void DestoryStack (SqStack *s) //销毁栈
{
free( s );
}
extern void IntStack (SqStack *S
void main()
{
ElemType e;
SqStack *s;
printf (" 栈s的基本运算如下;\n");
printf ("初始化栈\n");
printf ("栈为%s \n", (StackEmpty(s)?"空":"非空"));
printf ("依次进栈元素a,b,c,d,e\n");
Push(s, 'a');
Push(s, 'b');
Push(s, 'c');
Push(s, 'd');
Push(s, 'e');
printf ("栈为%s \n", (StackEmpty(s)?"空":"非空"));
printf ("出栈序列");
while (!StackEmpty(s))
{
Pop(s,e0;
printf("%c",e);
}
printf ("\n");
printf ("栈为%s\n", (StackEmpty(s)?"空":"非空"));
printf ("释放栈\n");
DestoryStack(s);
}
07-21