#define MAXSIZE 100
typedef int Datatype;
typedef struct {//對棧的定義
Datatype a[MAXSIZE];
int top;
}sequence_stack;
void init(sequence_stack *st)//棧的初始化
{
st->top = 0;
}
int empty(sequence_stack st)//判斷棧是否爲空
{
return (st.top ? 0 : 1);
}
Datatype read(sequence_stack st)//讀取棧頂元素
{
if (empty(st) == 1)
{
printf("棧爲空\n");
exit(1);
}
printf("棧頂元素爲%d\n", st.a[st.top - 1]);
}
void push(sequence_stack *st, Datatype x)//插入元素
{
if (st->top == MAXSIZE)
{
printf("棧已滿,無法進行插入操作!\n");
exit(1);
}
st->a[st->top] = x;
st->top += 1;
}
void dele(sequence_stack *st)//刪除棧頂元素
{
if (empty(*st) == 1)
{
printf("棧爲空,無法進行刪除操作!\n");
exit(1);
}
st->top -= 1;
}
void main()
{
sequence_stack S;
int i = 0;
init(&S);
printf("棧空爲1,非空爲0:%d\n", empty(S));
for (; i < 7; i++)
push(&S, i * 2);
printf("棧空爲1,非空爲0:%d\n", empty(S));
read(S);
dele(&S);
read(S);
}