Stack implementation (in C)
// Header file # include
/* = =========*/# Define MAX 100 # define OK 1 # define error 0 typedef struct Stack {int data [MAX]; int top; // Stack top int bottom; // STACK bottom} STACK, * stack; // initialize Stack int InitStack (STACK stack ); // destroy int DestroyStack (STACK stack); // clear int ClearStack (STACK stack STACK); // stack empty int StackEmpty (STACK stack STACK ); // obtain the STACK top element int GetTop (stack STACK stack, int * elem); // The pressure STACK int Push (stack STACK stack, int * elem ); // output STACK int Pop (stack STACK, int * elem); // return the length of elements in the stack int StackLength (STACK stack STACK, int * len ); // print the int PrintStack (STACK stack) element in the STACK; // stack full int StackFull (STACK stack STACK );
// # Include "stack. h "int main () {int num = 10, num1, I = 0, j = 0; Stack stack; int initFlag = InitStack (& stack); if (! InitFlag) return 0; for (num1 = 0; num1 <10; num1 ++) Push (& stack, & num1);/* int pushFlag = Push (& stack, & num); if (! PushFlag) return 0; * // ClearStack (& stack); for (; I <10; I ++) {int flag = Pop (& stack, & num1 ); if (flag) printf ("% d", num1); elseprintf ("Pop error");} int lenFlag = StackLength (& stack, & num1); if (lenFlag) printf ("len = % d", num1); elseprintf ("StackLength error");/* int getFlag = GetTop (& stack, & num1); if (getFlag) printf ("% d", num1); */PrintStack (& stack); return 0 ;}// initialize the STACK int InitStack (stack) {if (STACK = NULL) return error; stack -> Bottom = stack-> top = 0; return OK;} // The int Push (STACK stack, int * elem) {int flag = StackFull (stack ); if (! Flag) return error; stack-> data [stack-> top] = * elem; ++ stack-> top; return OK ;} // print all the elements in the STACK: int PrintStack (stack STACK) {int I = 0; int flag = StackEmpty (stack); if (! Flag) return error; for (I = 0; I
Top; I ++) {printf ("% d", stack-> data [I]);} printf ("\ n"); return OK ;} // STACK empty int StackEmpty (stack) {if (stack = NULL | STACK-> top = stack-> bottom) return error; elsereturn OK ;} // output STACK int Pop (stack, int * elem) {int flag = StackEmpty (STACK); if (! Flag) return error; * elem = stack-> data [stack-> top-1]; -- stack-> top; return OK ;} // STACK full int StackFull (stack) {if (stack = NULL | STACK-> top> MAX) return error; elsereturn OK ;} // destroy int DestroyStack (STACK stack) {if (stack = NULL) return OK; else {stack-> top = stack-> bottom; stack = NULL; return OK; }}// clear int ClearStack (STACK stack) {int flag = StackEmpty (STACK); if (! Flag) return error; else {stack-> top = stack-> bottom; return OK ;}// get the top element int GetTop (STACK stack, int * elem) {int flag = StackEmpty (stack); if (! Flag) return error; * elem = stack-> data [stack-> top-1]; return OK;} // return the int StackLength (STACK stack, int * len) {if (stack = NULL) return error; if (stack-> top = stack-> bottom) {* len = 0; return OK ;} else {* len = stack-> top; return OK ;}}