新手c语言顺序栈

自学数据结构中
栈中的顺序栈
#include “stdio.h”
#include “stdlib.h”
typedef struct Node
{
int *data;
int top;
int MAXSIZE;
}Seqstack;
Seqstack Initstack(int MAXSIZE)//置空栈
{
Seqstack s;
s=(Seqstack
)malloc(MAXSIZE
sizeof(struct Node));
s->top=-1;
return s;
}
int Emptystack(Seqstack *s)//判断是否成功创建
{
if(s->top==-1) return 1;
else return 0;
}
int Pushstack(Seqstack *s,int x,int max)//入栈
{
if(s->top==max-1) return 0;//栈满
else
{
s->top++;
s->data[s->top]=x;
return 1;
}
}
int Popstack(Seqstack *s,int *x)//出栈
{
if(Empty(s)) return 0;//已到栈底
else {
*x=s->data[s->top];
s->top–;
return 1;
}
}
int main()
{
Seqstack *s;
int max,x;
printf(“设定栈的容量\n”);
scanf("%d",&max);
s=Initstack(max);
Emptystack(s);
printf(“输入:\n”);
while(s->top!=max-1)
{
scanf("%d",&x);
Pushstack(s,x,max);
}
printf(“输出:\n”);
while(s->top!=-1)
{
Popstack(s,&x);
printf("%3d",x);
}
return 0;
}

发布了2 篇原创文章 · 获赞 4 · 访问量 1051

猜你喜欢

转载自blog.csdn.net/kitagawa_myouya/article/details/98615185