头文件
#include <stdio.h>
//#include <cstdlib>
#define MAXSIZE 100
typedef int datatype;
typedef struct{
datatype a[MAXSIZE];
int top;
}sequence_stack;//顺序栈的存储结构定义
//栈初始化
void init_stack(sequence_stack *st)
{
st->top=0;
}
//判断栈是否为空
int empty_stack(sequence_stack st)
{
return (st.top?0:1);//空则返回1,非空返回0
}
//读栈顶结点值
datatype read(sequence_stack st)
{
if (empty_stack(st)) {printf("\n栈是空的!");exit(1);}
else
return 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++;
}
//栈的删除
void pop(sequence_stack *st)
{
if (st->top==0)
{
printf("\n栈是空的!");
exit(1);
}
st->top--;
}
//栈的实际输入操作
void input_stack(sequence_stack *st)
{
if(st->top==MAXSIZE) {printf("\n栈是满的!");exit(1);}
printf("Please type some messages...type -88 to quit...\n");
while(st->a[st->top-1]!=-88)
{
scanf("%d",&st->a[st->top]);
st->top++;
}
st->top--;
}
//栈的元素的打印
void print_stack(sequence_stack st)
{
int i;
if (st.top==0) {printf("\n栈是空的!");exit(1);}
for (i=0;i<st.top;i++)
{
printf("[%d]=%d\n",i,st.a[i]);
}
}
【源文件】
#include<seqstack.h>
int main()
{
sequence_stack st;
init_stack(&st);
input_stack(&st);
print_stack(st);
return 0;
}