顺序栈及共享栈

来源:互联网 发布:铅弹可以在淘宝出售吗 编辑:程序博客网 时间:2024/04/28 15:22
typedef  int SElemType ;
#define MAXSIZE 100
typdef struct {
SElemType data[MAXSIZE];
int top;
}SqStack;


bool Push(SqStack *S, SElemType e)
{
if(S->top == MAXSIZE -1)
{
return false;
}
s->top ++;
S->data[S->top]  = e;
return true;
}


bool Pop(SqStack *S, ElemType *e)
{
if(S->top == -1)
{
return false;
}
*e = S->data[S->top];
S->top--;
return true;
}

//共享栈的结构定义


typdef struct {
SElemType data[MAXSIZE];
int top1;
int top2;
}SqDoubleStack;


bool Push(SqDoubleStack *S, SElemType e, int StackNumber)
{
if(S -> top1 + 1 == S -> top2)
return false;
if(StackNumber == 1)
{
S->data[++S->top1] = e; 
}
else if(StackNumber == 2)
{
S->data[S->top2] = e;
}
return true;
}




bool Pop(SqDoubleStack *S, SElemType *e, int StackNumber)
{
if(StackNumber == 1)
{
if(S->top1 == -1)
{
return false;
}
*e = S->data[S->top1--];
}
else if(StackNumber == 2)
{
if(S->top2 ==  -1)
{
return false;
}
*e = S->data[S->top2++];
}

return true;
}
0 0