新手c语言顺序栈

本文详细介绍了一种基于C语言的数据结构——顺序栈的实现过程。包括初始化空栈、判断栈是否为空、元素入栈、元素出栈等关键操作。通过具体代码示例,展示了如何在程序中使用顺序栈进行数据的暂存与读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自学数据结构中
栈中的顺序栈
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值