#include <stdio.h>#include<stdlib.h>structNode;typedefstructNode *ptrtonode;typedef Ptrtonode Stack;structnode{CharEle; Ptrtonode Next;}; Stackcreatestack (void) {Stack S; S=malloc(sizeof(structNode)); if(S = =NULL) printf ("Out of space"); S->next =NULL; returnS;}voidPush (Charch,stack S) {Ptrtonode Tmpcell; Tmpcell=malloc(sizeof(structNode)); if(Tmpcell = =NULL) printf ("Out of space"); Else{Tmpcell->next = s->Next; S->next =Tmpcell; Tmpcell->ele =ch; }}intIsEmpty (Stack S) {returnS->next = =NULL;}voidPop (Stack S) {Ptrtonode Tmpcell; Tmpcell= s->Next; S->next = tmpcell->Next; Free(Tmpcell);}CharTop (Stack S) {returnS->next->Ele;}intMain () {CharTmp; Stack S; S=Createstack (); while(TMP = GetChar ())! ='\ n' ) { if(TMP = ='['|| TMP = ='(') Push (tmp,s); Else if(TMP = =']') { if( !IsEmpty (S)) { Chartmp; TMP=Top (S); Pop (S); if(TMP! ='[') printf ("Error"); } Elseprintf ("Error"); }//else if Else { if( !IsEmpty (S)) { Chartmp; TMP=Top (S); Pop (S); if(TMP! ='(') printf ("Error"); } Elseprintf ("Error"); }//Else } if( !IsEmpty (S)) printf ("Error"); return 0;}
View Code
Algorithm Description: If you encounter open symbols into the stack, if you encounter a closed symbol, the first judgment is not empty stack, is an error, otherwise, on the stack to determine whether it is the corresponding open symbol, not an error, if read the end of the file, the stack is not empty, error
Stack application-stack is used to judge the balance symbol, [()] Right, [(]) the wrong