1. Two-stack Shared Space Structure
Typedef struct {SElemType data [MAXSIZE]; int top1;/* stack 1 top pointer */int top2;/* stack 2 top pointer */} SqDoubleStack;
2. Construct an empty stack S
Status InitStack(SqDoubleStack *S){ S->top1=-1; S->top2=MAXSIZE; return OK;}
3. Set S to empty Stack
Status ClearStack(SqDoubleStack *S){ S->top1=-1; S->top2=MAXSIZE; return OK;}
4. If Stack S is empty, TRUE is returned; otherwise, FALSE is returned.
Status StackEmpty(SqDoubleStack S){ if (S.top1==-1 && S.top2==MAXSIZE) return TRUE; else return FALSE;}
5. Return the number of elements of S, that is, the stack length.
int StackLength(SqDoubleStack S){ return (S.top1+1)+(MAXSIZE-S.top2);}
6. insert element e into the new stack top element.
Status Push (SqDoubleStack * S, SElemType e, int stackNumber) {if (S-> top1 + 1 = S-> top2)/* the stack is full, no more new elements can be pushed */return ERROR; if (stackNumber = 1) /* stack 1 has elements in the stack */S-> data [++ S-> top1] = e; /* if Stack 1 is used, first top1 + 1 and then assign a value to the array element. */Else if (stackNumber = 2)/* stack 2 has elements to stack */S-> data [-- S-> top2] = e; /* if Stack 2, first top2-1 and then assign a value to the array element. */Return OK ;}
7. If the stack is not empty, delete the stack top element of S, use e to return its value, and Return OK; otherwise, an ERROR is returned.
Status Pop (SqDoubleStack * S, SElemType * e, int stackNumber) {if (stackNumber = 1) {if (S-> top1 =-1) return ERROR; /* indicates that stack 1 is already an empty stack and overflows */* e = S-> data [S-> top1 --]; /* stack the top element of stack 1 out of the stack */} else if (stackNumber = 2) {if (S-> top2 = MAXSIZE) return ERROR; /* indicates that stack 2 is already empty, and overflow */* e = S-> data [S-> top2 ++]; /* exit the top element of stack 2 from the stack */} return OK ;}
8. display all elements
Status StackTraverse (SqDoubleStack S) {int I; I = 0; while (I <= S. top1) {visit (S. data [I ++]);} I = S. top2; while (I
Status visit(SElemType c){ printf("%d ",c); return OK;}
Reference <
<大话数据结构>
>