/*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;
}
数据结构-共享栈的基本操作实现
最新推荐文章于 2023-03-11 10:35:53 发布