顺序栈的基本操作

话不多说,直接上图
在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#define SElemType int
#define STACK_INIT_SIZE 100 
#define  STACKINCREMENT   10
#define OK 1
#define ERROR 0
void menu();
typedef struct {
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;
int InitStack (SqStack &S);
void DestroyStack(SqStack &S);   
void ClearStack(SqStack &S);  
void  StackEmpty (SqStack S); 
int StackLength (SqStack S);
void GetTop (SqStack S);   
int Push (SqStack &S,SElemType ); 
int Pop (SqStack &S, SElemType&); 
void putout(SqStack S) ;
void creatintput(SqStack &S);
int main()
{
	menu();	
	int select,m,n;
	SqStack L;
	while(1)
	{
		printf("请输入您的选择:\n");
		scanf("%d",&select);
		if(select<=0||select>11) 
			{
				printf("您的选择超出范围。");
				break;	
			}
		else 
			{
				switch(select)
				{
					case 1:	
							InitStack(L);
							break;
					case 2:
							DestroyStack(L);
					 		break;
					case 3: 
							ClearStack(L);
					 		break;
					case 4: 
							StackEmpty(L);
					 		break;
					case 5: 
							m=StackLength(L);
							printf("栈的长度为%d。",m);
							break;
					case 6:
							GetTop(L);
							break;
					case 7:
							printf("请输入插入元素:");
							scanf("%d",&m); 
							Push(L,m);
							break;
					case 8: 
							n=Pop(L,m);
							printf("被删除元素为:%d。",n);
							break;
					case 9: 
							putout(L);
							break;
					case 10:
							creatintput(L);
							break;
					case 11:
							return 0;
				}
			}			
	}
	
	return 0;
} 
void menu()
{	
	printf("***********************************************\n");
	printf("*********        1.初始化为空栈       *********\n");
	printf("********* 	 2.销毁栈             *********\n");
	printf("********* 	 3.将栈置空           *********\n");
	printf("********* 	 4.判断是否为空栈     *********\n");
	printf("*********	 5.返回栈长           *********\n");
	printf("********* 	 6.求栈顶元素         *********\n");
	printf("********* 	 7.插入元素设为栈顶   *********\n");
	printf("********* 	 8.删除元素并返回     *********\n");
	printf("********* 	 9.输出栈内元素       *********\n");
	printf("********* 	 10.创建并输入元素    *********\n");
	printf("********* 	 11.退出              *********\n");
	printf("***********************************************\n");
}
int InitStack (SqStack &S)
{
	S.base=(SElemType *) malloc (STACK_INIT_SIZE *sizeof (SElemType));
    if (!S.base)  {
    	printf("未成功分配!"); 
		return OK;
	} 
    S.top=S.base;
    S.stacksize= STACK_INIT_SIZE;
	printf("初始化成功!");    
return OK;
}
void DestroyStack(SqStack &S)
{
	S.top=NULL;
	S.stacksize=0;
	free(S.base);
	printf("销毁成功!"); 
}
void ClearStack(SqStack &S)
{
	S.top=S.base;
	printf("清空成功!"); 
} 
void  StackEmpty (SqStack S)
{
	if(S.top==S.base) printf("栈为空!");
	else printf("栈不为空!"); 
} 
int StackLength (SqStack S)
{
	return (S.top-S.base);
}
void GetTop (SqStack S)
{
	printf("栈顶元素为:%d",*(S.top-1)); 
}   
int Push (SqStack &S,SElemType P )
{
	if ( (S.top-S.base)>=S.stacksize )  
   {  
   		S.base=(SElemType *) realloc ( S.base,(S.stacksize+STACKINCREMENT)*sizeof (SElemType));
        if (!S.base)    
		{
			printf("未成功再次申请空间!");
			return ERROR;
		}
      S.top=S.base + S.stacksize;
      S.stacksize+=STACKINCREMENT;
    } 
    *S.top= P;
    ++S.top;
    printf("插入完成!");
    return OK;
} 
int Pop (SqStack &S, SElemType& P)
{
	int m;
	m=*(S.top-1);
	P=m;
	--S.top;
	return P;		
}   
void putout(SqStack S)
{
	
	while(S.top!=S.base)
	{
		printf("%d",*(S.base));
		++S.base;
	}
	printf("输出完成!"); 
}
void creatintput(SqStack &S)
{
	int m,n;
	InitStack(S);
	printf("请输入你要输入的个数:");	
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		printf("请输入你要输入的数:");
		scanf("%d",&n);	
		Push(S,n);	
	}	
	printf("插入完成!"); 
}

新手上路,勿喷。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值