C语言实现顺序栈

#include <stdio.h>
#include <stdlib.h>



#define MAX 100
#define OK 1
#define ERROR -1
#define STACK_SIZE 100 


typedef int Elemtype;

//顺序栈的结构描述 
struct Sq_stack 
{
	Elemtype stcak[STACK_SIZE];
	Elemtype top;
	
};
typedef struct Sq_stack Sqstack;



//初始化一个顺序栈
void InitSqstack(Sqstack &S)
{
	
	S.top=-1;
	printf("\n---success---\n");
	
} 


//入栈
void push(Sqstack &S,Elemtype data)
{
	if(S.top==STACK_SIZE-1)
	{
		printf("\n---FULL---\n");
		exit(ERROR);
	}
	else
	{
		
		S.top++;
		
		S.stcak[S.top]=data;
			
	}
} 


//出栈
Elemtype pop(Sqstack &S)
{
	Elemtype temp;
	
	if(S.top==-1)
	{
		printf("\n---Empty---\n");
	}
	
	else
	{
		temp=S.stcak[S.top];
		S.top--;
		return temp;
	}
	
} 


//栈的打印
void print_stack(Sqstack &S)
{
	Elemtype temp;
	
	printf("输出该堆栈:");
	
	if(S.top==-1)
	  printf("---Empty---\n");
	  
	  
	temp=S.top;
	
	
	while(S.top!=-1)
	{
		
		printf(" [%d] ",S.stcak[S.top]);
		S.top--;
		
	}
	
	S.top=temp;
} 



int main()
{
	Elemtype temp,temp1,temp2;
	
	
	Sqstack S;
	
	InitSqstack(S);
	
	do
	{
		printf("\n***请输入你想执行的功能***\n");
		printf("(1)进栈  (2)退栈   (3)EXIT\n");
		scanf("%d",&temp);
		
		switch(temp)
		{
			case 1:
			{
				printf("\n请输入你想进栈的数据:");
				scanf("%d",&temp1);
				
				push(S,temp1);
				print_stack(S);
				
				
			}break;
			
			
			case 2:
			{
				
				temp2=pop(S);
				printf("退栈的数据为:(%d)\n",temp2);
				
				print_stack(S);
				
			}break;
			
		}	
	}
	while(temp!=3);
	
	printf("****感谢您的使用****"); 
	printf("\n");
	
	
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值