结构
- #define MAXSIZE 20
- typedef int SElemType;
- typedef 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(top1 == -1){ //栈1为空
- return false;
- }
- *e = S->data[(S->top1)--];
- }
- else if(stackNumber == 2){
- if(S->top2 == MAXSIZE){ //栈2为空
- return false;
- }
- *e = S->data[(S->top2)++];
- }
- return true;
- }