Recent data structures and algorithms to participate in final exams, summarize here some of the routine operation of the stack onto the stack.
#include <stdlib.h> #include " stdio.h " #include < String .h> typedef char SElemType; // type stack data elements #define STACK_INIT_SIZE 10 // initial allocation of storage space in the stack #define STACKINCREMENT 10 / / dispensing increments of the stack memory space typedef struct { SElemType * Base ; // before and after construction and destruction, base value NULL SElemType * Top; // stack pointer int STACKSIZE; //Storage capacity currently allocated (in sizeof (SElemType) units) } Stack; char InitStack (Stack & S) { S. Base = (SElemType *) malloc (* STACK_INIT_SIZE the sizeof (SElemType)); // by malloc function allocates space IF (S!. Base ) return 0 ; // if the allocation fails, returns zero . S.top = S Base ; // stack pointer is set to address the stack bottom S.stacksize = STACK_INIT_SIZE; // space currently allocated return . 1 ; } char StackEmpty (Stack S) { IF (S. Base ) { IF (S. Base == S.top) // if the stack is empty stack Returns 1 (true), otherwise 0 (to false) { return . 1 ; } } return 0 ; } char the Push (stack & S, SElemType E) { IF (S. Base ) { IF ((S-S.top. Base )> = S.stacksize) // stack is full, the additional storage space { S. Base = (* SElemType ) realloc (S. Base , (+ S.stacksize STACKINCREMENT) * the sizeof (SElemType)); IF (S. Base == NULL) return 0 ; // if space reallocation fails, the return 0 (false), represents an unsuccessful insertion S.top . S = Base + S.stacksize; // new address array disposed S.stacksize + = STACKINCREMENT; // allocate space to add additional space } * S.top ++ = E; return . 1 ; } return 0 ; } / / * S.top ++ = E //++ position top of the stack, the stack space present calculation S.top unchanged, S is the stack pointer to point E, only stack space operation after the completion of the self-energizing. 1 // * = E + S.top // Stack ++ top position before, this operation forward S.top stack 1 increases, the stack S is the new stack point E. char Pop (Stack & S, SElemType & E) { IF (S. Base ) { IF (S.top> S. Base ) // if the stack is not empty { E = * - S.top; return . 1 ; } } return 0 ; } char StackTraverse (stack & S) // sequentially output from each element of the stack to the bottom of the stack { intI; IF (S. Base ) { the printf ( " \ n-sequentially outputted from the stack to the bottom of each stack element is: \ n- " ); for (I = 0 .; I <S.top S- Base ; I ++ ) { the printf ( " % C " , S. Base [I]); // sequentially print data elements } the printf ( " \ n- " ); return . 1 ; } return 0 ; } void main () { Stack S; char x,y; InitStack(S); x='c';y='k'; Push(S,x);StackTraverse(S); Push(S,'a');StackTraverse(S); Push(S,y);StackTraverse(S); Pop(S,x);StackTraverse(S); Push(S,'t');StackTraverse(S); Push(S,x);StackTraverse(S); Pop(S,x);StackTraverse(S); Push(S,'s');StackTraverse(S); while(!StackEmpty(S)){ Pop(S,y);StackTraverse(S); printf("%c",y); } printf("%c\n",x); }
Reference code written by someone else, the code yourself again knocked again. But still are not familiar with.
A resuscitation, two back cooked it! A lot of practice will be able to grasp.