#include<stdio.h>
#define MaxSize 20
#define ERROR 0
#define OK 1
typedef int ElementType;
typedef struct DStack *Stack;
//共享栈
struct DStack
{
ElementType Data[MaxSize];
int Top1; //堆栈1的栈顶指针
int Top2; //堆栈2的栈顶指针
}S;
/**
* 存放数据
* */
void Push(Stack PtrS,ElementType item, int Tag)
{
if(PtrS->Top2 - PtrS->Top1 == 1)
{
printf("堆栈满");
return ;
}
if(Tag == 1)
{
PtrS->Data[++(PtrS->Top1)] = item;
}
else
{
PtrS->Data[--(PtrS->Top2)] = item;
}
}
/**
* 出栈
* */
ElementType Pop(Stack PtrS,int Tag)
{
if(Tag == 1)
{
//对左面的的栈进行出栈
//出栈前判断是否为空
if(PtrS->Top1 == -1)
{
printf("堆栈1为空");
}
else
{
return PtrS->Data[(PtrS->Top1)--]
}
}
else
{
//对右面的栈进行出栈
//出栈前判断是否为空
if(PtrS->Top2 == MaxSize)
{
printf("堆栈2为空");
}
else
{
return PtrS->Data[(PtrS->Top2)++];
}
}
}
c语言数据结构共享堆栈
最新推荐文章于 2024-06-06 23:13:09 发布