内容:实现顺序栈的各种基本运算,完成以下功能
1.初始化栈s
2.判断栈s是否为空
3.依次进栈元素a,b,c,d,e
4.判断栈s是否非空
5.输出出栈序列
6.判断栈s是否非空
7.释放栈
#include<stdio.h>
#include<malloc.h>
#define Maxsize 50
typedef char ElemType;
char Z;
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;
printf("顺序栈的基本运算\n");
printf("初始化栈\n");
InitStack(s);
printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空"));
printf("依次进栈元素a,b,c,c,d,e\n");
Push(s, 'a');
Push(s, 'b');
Push(s, 'c');
Push(s, 'd');
Push(s, 'e');
printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空"));
printf("依次出栈");
while (s->top!=-1)
{
Pop(s, Z);
printf("%c", Z);
}
printf("\n");
printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空"));
printf("释放栈\n");
DestroyStack(s);
}