Said
typedefstruct{data_t*data;/*storage of the stack, * in array, dynamic allocated*/ intTop/*Current top of the stack, the value of * top is the index of the data array. * Note the type of stack we create here * are "full-stack", which means the top always * points to The location is which the last * item is stored. A push would first * increment the stack pointer, then store * the value. Another stack type is called * "Empty-stack": of which the top points to * the location in which The next item would be * stored. A push would first store the value, * and then increment the stack pointer. */ intmax_depth;/*max depth of stack, initialized when the * stack was created, used to limit top * not E Xceed the max depth*/} sqstack_t;
Realize
//Createsqstack_t *createemptysqstack (intmax_depth) {sqstack_t*Stack; if(Max_depth = =0) returnNULL; Stack= (sqstack_t *) malloc (sizeof(sqstack_t)); if(Stack = =NULL)returnNULL; Stack->data = (data_t *) malloc (sizeof(data_t) *max_depth); if(Stack->data = =NULL) {free (stack); returnNULL; } Stack->top =-1; Stack->max_depth =max_depth; returnStack;}//ClearintClearsqstack (sqstack_t *stack) { if(NULL! =stack) {Stack->top =-1; return 0; } Else { return-1; }}//destroyedintDestroysqstack (sqstack_t *stack) { if(NULL! =stack) { if(Stack->data! =NULL) Free (stack-data); Free (stack); return 0; } Else { return-1; }}//is emptyintEmptysqstack (sqstack_t *stack) { if(NULL! =stack) { return(-1= = Stack->top?1:0); } Else { return-1; }}//is fullintFullsqstack (sqstack_t *stack) { if(NULL! =stack) { return(Stack->max_depth-1) = = Stack->top?1:0 ); } Else { return-1; }}//Press StackintPushstack (sqstack_t *stack, data_t x) { if(NULL = = stack)return-1; if(Fullsqstack (Stack))return-1; Stack->top++; Stack->data[stack->top] =x; return 0;}//out of the stackintPopstack (sqstack_t *stack, data_t *x) { if(NULL = = stack)return-1; if(Emptysqstack (Stack))return-1; if(NULL = = x)return-1; *x = stack->data[stack->top]; Stack->top--; return 0;}//get top of StackintGetTop (sqstack_t *stack, data_t *x) { if(NULL = = stack)return-1; if(NULL = = x)return-1; if(Emptysqstack)return-1; *x = stack->data[stack->top]; return 0;}
Test code
intPush_pop (sqstack_t *stack, data_t x);/** Iterate through the stack, from the base to the top * and print out info for each element*/voidIterate_stack (sqstack_t *stack) { inti; if(!stack)return; printf ("stack = base{"); if(Stack->data) {/*Just for protection*/ for(i =-1; I < stack->top;) {printf ("%d,", stack->data[++i]); } } if(1==emptysqstack (Stack)) printf ("}top\n"); Elseprintf ("\b}top\n");}intMainintargcChar*argv[]) {sqstack_t*Stack; intmax_depth; if(ARGC <2) {printf ("Usage:%s <max_depth>\n", argv[0]); return-1; } max_depth= Atoi (argv[1]); Stack=Createemptysqstack (max_depth); if(!stack) {printf ("Createemptysqstack error\n"); return-1; } push_pop (Stack,1); Destroysqstack (stack); return 0;}intPush_pop (sqstack_t *stack, data_t x) {data_t data_pop; if(Fullsqstack (Stack)) {printf ("-----reach the max depth of the stack!\n"); return 0; } Else{printf ("Push%d\n", x); Pushstack (Stack, x++); Iterate_stack (stack); Push_pop (Stack, x); Popstack (Stack,&Data_pop); printf ("Pop%d\n", Data_pop); Iterate_stack (stack); return-1; }}
Results
Push1Stack=Base{1}toppush2Stack=Base{1,2}toppush3Stack=Base{1,2,3}toppush4Stack=Base{1,2,3,4}toppush5Stack=Base{1,2,3,4,5}toppush6Stack=Base{1,2,3,4,5,6}toppush7Stack=Base{1,2,3,4,5,6,7}toppush8Stack=Base{1,2,3,4,5,6,7,8}top-----reach the max depth of the stack!Pop8Stack=Base{1,2,3,4,5,6,7}toppop7Stack=Base{1,2,3,4,5,6}toppop6Stack=Base{1,2,3,4,5}toppop5Stack=Base{1,2,3,4}toppop4Stack=Base{1,2,3}toppop3Stack=Base{1,2}toppop2Stack=Base{1}toppop1Stack=Base{}top
The representation and implementation of sequential stacks