1. Ordered stack structure
Typedef struct {SElemType data [MAXSIZE]; int top;/* for top stack pointer */} SqStack;
2. Construct an empty stack S
Status InitStack(SqStack *S){ /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */ S->top=-1; return OK;}
3. Set S to empty Stack
Status ClearStack(SqStack *S){ S->top=-1; return OK;}
4. If Stack S is empty, TRUE is returned; otherwise, FALSE is returned.
Status StackEmpty(SqStack S){ if (S.top==-1) return TRUE; else return FALSE;}
5. Return the number of elements of S, that is, the stack length.
int StackLength(SqStack S){ return S.top+1;}
6. If the stack is not empty, use e to return the top element of S stack and Return OK; otherwise, an ERROR is returned.
Status GetTop(SqStack S,SElemType *e){ if (S.top==-1) return ERROR; else *e=S.data[S.top]; return OK;}
7. insert element e as the new top element of the stack.
Status Push (SqStack * S, SElemType e) {if (S-> top = MAXSIZE-1)/* Full stack */{return ERROR ;} s-> top ++;/* Add a */S-> data [S-> top] = e to the top pointer of the stack; /* assign the new inserted element to the top space of the stack */return OK ;}
8. 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 (SqStack * S, SElemType * e) {if (S-> top =-1) return ERROR; * e = S-> data [S-> top]; /* the top element of the stack to be deleted is assigned to e */S-> top --;/* the top pointer of the stack minus one */return OK ;}
9. display each element in the stack from the bottom of the stack to the top of the stack.
Status StackTraverse(SqStack S){ int i; i=0; while(i<=S.top) { visit(S.data[i++]); } printf("\n"); return OK;}
Status visit(SElemType c){ printf("%d ",c); return OK;}
Reference < <大话数据结构> >