C语言顺序栈的操作

#include <stdio.h>
#include <stdlib.h>
#define ElemType int
#define MaxSize 50
typedef struct{
	ElemType data[MaxSize];
	int top;
}SqStack;
void InitStack(SqStack *s){//初始化栈 
	s->top=-1;
}
void StackEmpty(SqStack s){//判断栈是否为空 
	if(s.top==-1){
		printf("栈空\n"); 
	}
}
void Push(SqStack *s){//进栈 
	ElemType x;
	if(s->top==MaxSize-1){
		printf("栈满\n"); 
	}
	printf("请依次输入你要进栈的元素:");
	scanf("%d",&x);
	while(x !=9999){
		s->data[++s->top]=x;
		scanf("%d",&x);
	}
}
void Pop(SqStack *s,int i){//出栈 
	 ElemType x;
	 if(s->top==-1){
	 	printf("栈空\n"); 
	 } 
	 printf("你出栈的元素依次为:"); 
	 for(int j=1;j<=i;j++){
	 	x=s->data[s->top--];
	 	printf("%5d",x);
	 }
	 printf("\n");
		 
}
void PrintStack_Push(SqStack s){//进栈后遍历 
	if(s.top==-1){
		printf("栈空\n"); 
	} 
	printf("进栈后自底向上遍历栈:");
	for(int i=0;i<=s.top;i++){
		printf("%5d",s.data[i]);
	}
	printf("\n");
}
void PrintStack_Pop(SqStack s){//出栈后遍历 
	if(s.top==-1){
		printf("栈空\n"); 
	} 
	printf("出栈后自底向上遍历栈:");
	for(int i=0;i<=s.top;i++){
		printf("%5d",s.data[i]);
	}
	printf("\n");
}
int GetTop(SqStack s){//读取栈顶元素 
	ElemType x;
	if(s.top==-1){
			printf("栈空\n"); 
	} 
	x=s.data[s.top];
	return x;
} 
int main(){
	SqStack s;
	InitStack(&s);
	StackEmpty(s);
	Push(&s);
	PrintStack_Push(s);
	Pop(&s,3);
	PrintStack_Pop(s);
	printf("%d",GetTop(s));
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值