# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <math. h> # define true 1 # define false 0 # define error 0 # define Infeasible-1 typedef int status; typedef int Boolean; typedef int selemtype; # define stack_init_size 10 # define stack_increment 2 struct sqstack {selemtype * base; selemtype * Top; selemtype stacksize;}; void initstack (sqstack & S) {If (! (S. base = (selemtype *) malloc (stack_init_size * sizeof (selemtype) Exit (overflow); // storage allocation failed S. top = S. base; S. stacksize = stack_init_size;} void destroystack (sqstack & S) {free (S. base); S. base = NULL; S. top = NULL; S. stacksize = 0;} void clearstack (sqstack & S) {S. top = S. base;} status stackempty (sqstack s) {If (S. top = S. base) return true; else return false;} int stacklength (sqstack s) {return S. top-S.base;} status Gettop (sqstack S, selemtype & E) {If (S. top> S. base) {e = * (S. top-1); Return OK;} else return error;} void push (sqstack & S, selemtype e) {If (S. top-S.base> = S. stacksize) {S. base = (selemtype *) realloc (S. base, (S. stacksize + stack_increment) * sizeof (selemtype); If (! S. base) Exit (overflow); S. top = S. base + S. stacksize; S. stacksize + = stack_increment;} * (S. top) ++ = E;} status POP (sqstack & S, selemtype & E) {If (S. top = S. base) Return Error; E = * -- S. top; Return OK;} void stacktraverse (sqstack S, void (* visit) (selemtype )) {// call the visit () while (S. top> S. base) visit (* s. base ++); printf ("\ n");} void print (selemtype c) {printf ("% d", c);} void main () {Int J; sqstack s; se Lemtype E; initstack (s); For (j = 1; j <= 12; j ++) Push (S, J); printf ("the elements in the stack are: "); stacktraverse (S, print); POP (S, e); printf (" pop-up top stack Element E = % d \ n ", e ); printf ("Stack null No: % d (1: NULL 0: No) \ n", stackempty (s); gettop (S, e ); printf ("Stack top Element E = % d stack length: % d \ n", E, stacklength (s); clearstack (s); printf ("after clearing the stack, stack empty no: % d (1: NULL 0: No) \ n ", stackempty (s); destroystack (s); printf (" after the stack is destroyed, S. top = % u s. base = % u s. stacksize = % d \ n ", S. top, S. base, S. stacksize );/ /Value conversion printf ("\ n ************************ number conversion ****** * ********************** \ n "); sqstack num; initstack (Num); int N; printf ("Enter N: \ n"); scanf ("% d", & N); While (N) {push (Num, N % 8); n = N/8;} while (! Stackempty (Num) {Pop (Num, e); printf ("% d", e );}}
Printf ("\ n ");
Data Structure-basic operations and Applications of stacks (digital conversion)