A typical application of stacks is the ability to assist in parsing the parentheses of an expression. Brackets can be extended to any paired occurrences of the delimiter, such as quotation marks, title numbers, and so on.
Next, we give the program implementation:
The first part gives the implementation of the INSERT, delete, and other operations of the stack:
1 Public classStackchar2 {3 Private intMaxSize;//Stack array size4 Private Char[] stackarray;5 Private intTop//Stack Top6 PublicStackchar (intmaxSize)7 {8 This. maxsize=maxsize;//To set the initialization size of an array9stackarray=New Char[maxSize];TenTop=-1; One } A Public voidPushCharch) - { - //into the stack the if(Isfull ()) -SYSTEM.OUT.PRINTLN ("Cannot insert item" +CH + "! The stack is already full. "); - Else - { +top++; -stackarray[top]=ch; + } A } at Public CharPopCharch) - { - //into the stack - if(IsEmpty ()) - { -System.out.println ("Cannot delect item" +ch + "! The stack is empty. "); in return0; - } to Else + { - CharCh=Stackarray[top]; thestackarray[top]=NULL; *top--; $ returnCh//out stack and return valuePanax Notoginseng } - } the Public intsize () + { A returnTop+1; the } + Public intPeek () - { $ returnStackarray[top]; $ } - Public CharNumintN) - { the returnStackarray[n];//returns the value of index n - }Wuyi Public BooleanIsEmpty () the { - return( -1==top); Wu } - Public Booleanisfull () About { $ return(maxsize-1==top); - } - Public voidprint () - { ASystem.out.println ("Stack:"): + for(inti=0;i<maxsize;i++) the { - System.out.println (num (i)); $ } the } the}
The second part is to give the corresponding parentheses to judge:
1 Public classbrackcheck{2 PrivateString instring;3 PublicBrackcheck (String in) {4instring=in ;5 }6 Public voidCheck () {//Check if parentheses match or not7 {8 intlen=instring.length ();9Stackchar chstack=NewStackchar (len);Ten Charch; One CharChpop; A Booleanflag=false; - for(intindex=0; index<len;index++){ -Ch=Instring.charat (index); the Switch(CH) { - Case‘{‘: - Case‘[‘: - Case‘(‘: +Chstack.push (CH);//if the first half of the parentheses are detected, the stack operation is performed - Break; + Case‘}‘: A Case‘]‘: at Case‘)‘: - if(!chstack.isempty ()) {//anti-parenthesis detected -chpop=Chstack.peek (); - if((' {' {' ==chpop && '} '!=ch)//determine if the parentheses at the top of the stack match the current read -in parenthesis -|| (' [' ==chpop && '] '! =ch) -|| (' (' ==chpop && ') '! =ch)) in{//no match on the error -System.out.println ("Error:" + ch+ "at" +index); toflag=true; + } - Else if(' {' {' ==chpop && '} ' = =ch) the|| (' [' ==chpop && '] ' = =ch) *|| (' (' ==chpop && ') ' = =ch)) $ //match, Stack, make next characterPanax Notoginseng Chstack.pop (); - } the Else//An error occurred while the stack was empty when an anti-parenthesis was encountered + { ASystem.out.println ("Error:" +ch+ "at" +index); theflag=true; + } - default: $ Break; $ } - } - if(!flag) theSystem.out.println ("The delimiters of the string is matching!"); - }Wuyi}
Application of stacks-judging bracket matching