//两栈的共享空间结构,只针对同类型
typedef struct
{
SElemType data[MAXSIZE];
int top1; //栈1栈顶指针
int top2; //栈2栈顶指针
}SqDoubleStack;
//插入新元素e到栈顶
Status Push (SqDoubleStack *S,SElemType e,int stackNumber)
{
if (S->top1+1 == S->top2) //栈满
return ERROR;
if (stackNumber == 1) //栈1有元素进栈
S->data[++S->top1] = e; //先top1+1再赋值
else if (stackNumber == 2)
S->data[--S->top2] = e; //先top2-1再赋值
return OK;
}
//删除栈顶元素,用e返回值
Status Pop (SqDoubleStack *S,SElemType e,int stackNumber)
{
if (stackNumber == 1)
{
if (S->top1 == -1)
return ERROR;
*e = S->data[S->top1--]; //将栈1的元素出栈
}
else if (stackNumber == 2)
{
if (S->top2 == MAXSIZE)
return ERROR;
*e = S->data[S->top2++]; //说明2空栈
}
return OK;
}
两栈的共享空间结构
最新推荐文章于 2023-04-01 15:29:27 发布