//------------------- stack2.cpp ------------------------- //------------ source file for unit stack ---------------- // #include #include #include #include "stack2.h" //--------------------------- Iplementstion ----------------------------- //------------------ stack_init ------------------------- stack_type stack_init (void) { stack_type S; S = NULL ; return S; } //------------------- stack_push (&S, x) ---------------- void stack_push (stack_type &S, stack_info_type x) { stack_type q; q = (stack_type)malloc(sizeof(struct link_type)); q->info = x; q->next = S ; S = q; } //-------------------- stack_pop (&S) --------------------- stack_info_type stack_pop (stack_type &S) { stack_type q ; stack_info_type x ; q = S ; S = S->next ; x = q->info ; free (q) ; return x ; } //-------------------- stack_top (S) ---------------------- stack_info_type stack_top (stack_type S) { return (S->info); } //-------------------- stack_empty (S) -------------------- int stack_empty (stack_type S) { return (S==NULL) ; } //-------------------- stack_show (S) --------------------- void stack_show (stack_type S) { stack_type S1 ; stack_info_type x ; S1 = stack_init () ; printf ("Top --> ") ; while (! stack_empty (S)) { x = stack_pop (S) ; printf("%d ",x); stack_push (S1, x) ; } while (! stack_empty (S1)) { x = stack_pop (S1) ; stack_push (S, x) ; } printf (" // \n \n") ; }