bracket matching [Stack]

Source: Internet
Author: User

title: The parentheses of the string [(())] match, the problem is relatively simple, also recorded.
Idea, the left parenthesis on the stack, encountered the right parenthesis on the stack an element to match the decision, construct a switch. At the same time, set a monitoring flag, the initial 1, once the mismatch will be placed to 0, that is, later no longer compare.

#include <iostream>using namespacestd;#defineMaxSize 10typedefstructstack{CharA[maxsize]; inttop;} Stack,*Stack;voidPush (Stack &s,CharCh/*into the stack*/{s->a[++s->top]=ch;}CharPop (Stack &s)/*out of the stack*/{    Charx=s->a[s->top--]; returnx;}intMain () {intI=0, flag=1; Charx; Char*string="[(((())))]"; Stack s= (Stack)malloc(sizeof(stack)); S->top=-1;  while(string[i]!=' /')    {        Switch(string[i])/*left parenthesis in stack, right parenthesis out stack*/        {             Case '(': Push (S,'(');  Break;  Case '[': Push (S,'[');  Break;  Case ')':                       if((X=pop (s))! ='(')/*match judgment after the stack is out*/Flag=0;  Break;  Case ']':            if((X=pop (s))! ='[') Flag=0;  Break; } I++; if(!flag)/*exit the loop as long as there is a mismatch*/         Break;                 }; if(flag) printf ("Mattch"); Elseprintf ("No Mattch"); return 0; }

bracket matching [Stack]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.