This section provides a testCodeAnd description.
Int main (void) {# define max_sizemax_node # define val_range1_int I, Val; stack * pstack = NULL; printf ("Enter % s: \ n", _ FUNC __); pstack = create (); If (pstack = NULL) {perror ("create stack failed! \ N "); Return-1;} printf (" \ npush data: \ n "); for (I = 0; I <max_size; I ++) {if (I! = 0 & I % line_num = 0) printf ("\ n"); printf ("% d \ t", I); push (pstack, & I );} # If 0 printf ("\ n \ ndump stack % d Data: \ n", pstack-> size); dump_stack (pstack ); # endif printf ("\ n \ NPOP data: \ n"); for (I = 0; I <max_size; I ++) {if (I! = 0 & I % line_num = 0) printf ("\ n"); If (! Pop (pstack, & Val) printf ("% d \ t", Val); elsebreak;} printf ("\ n \ ndump stack data: \ n "); dump_stack (pstack); // destory the stack; destory_stack (pstack); printf ("\ nmain end! \ N "); Return 0 ;}
Stack implementation is relatively simple. Generally, it can be implemented using arrays or linked lists. The advantage of array implementation is that the address of each element is continuous, but the disadvantage is that the number of nodes in the stack must be known first; the stack implemented by chain can dynamically increase, you can flexibly define the data field type (void * Data). In actual use, you only need to specify the required type for the "Void *" type.
Full code: Download