数据结构-共享栈的基本操作实现

/*main*/
#include<stdio.h>
#include<stdlib.h>
#define MaxSize  4
#define TRUE       1
#define ERROR      0
#define OK         1
#define FALSE      0
#define OVERFLOW   -2
typedef int ElemType;
typedef int status;
typedef struct Stack{
	ElemType Data[MaxSize];
	int top1;
	int top2;
}*SqStack,Stack;
#include"SeqStack.h"
int main()
{

	Stack * s=(SqStack)malloc(sizeof(Stack));;
 	Create(s);
 	print(s);
 	putchar(10);
 	int flag1,flag2;ElemType e,x;
 	printf("请输入要入栈的标号和元素:");
 	scanf("%d",&flag1);
 	scanf("%d",&e);
 	push(s,flag1,e);
 	print(s);
 	putchar(10);
 	printf("请输入要出栈的栈标号:");
 	scanf("%d",&flag2);
 	Pop(s,flag2);
 	putchar(10);
 	print(s);
	return 0;
}
/*要包含的的头文件*/
status Create(Stack *S)
{
	S->top1=-1;
	S->top2=MaxSize;
	S->top1++;
	S->Data[S->top1]=2;
	S->top1++;
	S->Data[S->top1]=78;
	S->top2--;
	S->Data[S->top2]=90;
	S->top2--;
	S->Data[S->top2]=908;
	return OK;
}

status print(Stack *S)
{
	if(S->top1==-1&&S->top2==MaxSize)
	return ERROR;
	printf("top1:");
	for(int i=0;i<S->top1+1;i++)
	{
		printf("%d ",S->Data[i]);
	}
	printf("\ntop2:");
	for(int i=S->top2;i<MaxSize;i++)
	{	
		printf("%d ",S->Data[i]);
	}
	printf("\n");
}

status push(Stack *S,int flag,ElemType e)
{
	if(S->top1+1==S->top2)
	{
		printf("栈满!\n");
		return ERROR;
	}
	
	if(flag==1)
	{
		S->top1++;
		S->Data[S->top1]=e;
	}
	else if(flag==2)
	{
		S->top2--;
		S->Data[S->top2]=e;
	}
	
	return OK;	
}
status Pop(Stack *S,int flag)
{
	ElemType e; 
	if(S->top1==-1&&S->top2==MaxSize)
	{
		printf("栈空!");
		return ERROR;
	}
		if(flag==1)
	{
		e=S->Data[S->top1];
		S->top1--;
		
	}
	else if(flag==2)
	{	
		e=S->Data[S->top2];
		S->top2++;	
	}
	else
		printf("输入错误!");
		
	printf("pop出的元素:%d",e);
	return OK;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值