Sequential Stack
# Include <stdio. h> // initialize the stack [sequential Stack] void initstack (seqstack * s) {S-> Top =-1 ;}// stack-in-stack int push (seqstack * s, stackelementtype X) {If (S-> Top = Stack_size-1) {return false;} s-> top +; s-> ELEM [S-> top] = X; return true;} // output stack int POP (seqstack * s, stackelementtype * X) {If (S-> Top =-1) {return false ;} else {* x = s-> ELEM [S-> top]; S-> top --; return true ;}// get the top element of the stack int gettop (seqstack * s, stackelementtype * X) {If (S-> Top =-1) {return false;} else {* x = s-> ELEM [S-> top]; return true ;}} int main (void) {return 0 ;}
Chain Stack
# Include <stdio. h> typedef struct node {stackelementtype data; struct node * Next;} linkstacknode; typedef linkstacknode * linkstack; // stack-in-stack [Chain Stack] int push (seqstack top, stackelementtype X) {linkstacknode * temp; temp = (linkstacknode) malloc (sizeof (linkstacknode); If (temp = NULL) // return false if the application fails; temp-> DATA = X; temp-> next = Top-> next; top-> next = temp; return true;} // output stack [Chain Stack] int POP (seqstack top, stackelementtype * X) {linkstacknode * temp; temp = Top-> next; If (temp = NULL) // the stack is empty, return false; top-> next = temp-> next; * x = temp-> data; free (temp); Return true;} int main (void) {return 0 ;}
Bracket Matching Algorithm
# Include <stdio. h> // bracket matching algorithm void bracketmatch (char * Str) // The input string {stack S, int I, char ch; initstack (& S ); for (I = 0; STR [I]! = '\ 0'; I ++) {Switch (STR [I]) {Case' (': Case' [': Case' {': Push (& S, STR [I]); break; Case ')': Case ']': Case '}': If (isempty (& S )) {printf ("unnecessary parentheses"); return;} else {gettop (& S, & Ch); If (MATCH (CH, STR [I]) pop (& S, & Ch); elseprintf ("corresponding to different brackets") ;}}if (isempty (& S) printf ("matching brackets "); elseprintf ("Parentheses do not match");} int main (void) {return 0 ;}