#include "stdio.h"
#include<stdbool.h>
#include "stdlib.h"
#define MaxSize 20
typedef char ElemType;
typedef struct
{
ElemType a[MaxSize];
int top;
} SeqStack;
void Initstack(SeqStack *&s) //初始化 **这种地方使用&就会报错,但是在devc++中就可以使用。**
{
{
s = (SeqStack *)malloc(sizeof(SeqStack));
s->top = -1;
}
bool Push(SeqStack *&s, ElemType e) //入栈 **这种地方使用&就会报错,但是在devc++中就可以使用**。
{
{
if (s->top == MaxSize - 1)
;
return false; //返回值也要打;
s->top++; //先加,因为top的初始位是-1。
s->a[s->top] = e;
return true;
}
bool Pop(SeqStack *&s, ElemType &e) //出栈 **这种地方使用&就会报错,但是在devc++中就可以使用**。
{
if (s->top == -1)
return false;
e = s->a[s->top];
s->top--;
return true;
}
bool StackEmpty(SeqStack *s)
{
if (s->top == -1)
return false;
else
return true;
}
int main()
{
SeqStack *s;
Initstack(s);
Push(s, 'a'); // ASCII码需要打' '
Push(s, 'b');
Push(s, 'c');
Push(s, 't');
Push(s, 'x');
printf("栈为%s\n", StackEmpty(s) ? "空" : "非空"); // c语言98页
ElemType e;
Pop(s, e);
printf("出栈的数为%c\n", e);
Pop(s, e);
printf("出栈的数为%c\n", e);
Pop(s, e);
printf("出栈的数为%c\n", e);
Pop(s, e);
printf("出栈的数为%c\n", e);
}