One of the classic applications of stacks: bracket matching check __ data structure

Source: Internet
Author: User

features: Mainly matches the parentheses in the string (including "{}", "[]", "()"), and when the match succeeds, the print match succeeds or the print match fails.

Algorithm idea:
Start the scan from the first character of the string, if the normal character is ignored, if the left symbol is the stack operation, and when the right symbol is encountered, pops the top element from the stack and matches it;

At the end of the match:
Success: All characters were scanned and the stack was empty;
Failed: Match failed or all character scan completed but stack is not empty;

Attaching: The debugging process, some small problems, have time to revise;

. h file #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE 1 #define Fals
E 0 #define SIZE typedef char ELEMTYPE;
    typedef struct STACK {elemtype *base;
    int top;
  int stacksize;

}stack;
1. Create an empty stack void Init (stack *s);
2. into the stack int Push (stack *s,elemtype D);
3. Stack int Pop (stack *s,elemtype *mptr);
4. Get stack top element int GetTop (stack *s,elemtype *e);
5. Judge stack non-null int Empty (stack *s);
6. Print stack element void List (stack *s);


7. Match bracket int MyFunc (char *mstring);
    The. c file #include "Stack.h"//1. Create an empty stack void Init (Stack *s) {s->base= (Elemtype *) malloc (sizeof (elemtype) *size);
    s->top=0;
  s->stacksize=size;
  return;  //2. into stack int Push (stack *s,elemtype D) {//Stack full if (s->top>=s->stacksize) {s->base= (elemtype
         *) ReAlloc (s->base,sizeof (s->stacksize+1) *sizeof (elemtype));
      Determine if stack memory space is allocated successfully if (S->base==null) return false;
    s->stacksize++; } S-&GT;BASE[S-&GT;top++]=d;
  return true;
   //3. stack int Pop (stack *s,elemtype *mptr) {if (s->top==0) return false;
      else {*mptr=s->base[s->top--];
     return true;
    //4. Gets the top element int GetTop (stack *s,elemtype *e) {if (s->top==0) return false;
    *e=s->base[--s->top];
  return true;
     //5. Judgment stack non-null int Empty (stack *s) {if (s->top==0) return true;
  return false;
  //6. Print stack element void List (Stack *s) {if (s->top==0) return;
     while (s->top-1>=0) {printf ("data:%c\n", s->base[s->top-1]);
    s->top--;
    //7. Match parentheses int MyFunc (char *mstring)//mstring->[26* (12+8)/(45-29)] {int i=0,flag=1;
    Stack S1;
    Elemtype e;
    Init (&AMP;S1);
        while (mstring[i]!= ' ") {switch (Mstring[i]) {case ' (':P ush (' &s1, ' (');
        Case ' [':P ush (&AMP;S1, ' [];
        Case ' {':P ush (&s1, ' {'); Case ') ': GetTop (&s1,&amP;E);
          if (e== ' (') Pop (&s1,&e);
          else flag=0;
        Break
          Case '] ': GetTop (&s1,&e);
           if (e== ' [') Pop (&s1,&e);
          else flag=0;
        Break
          Case '} ': GetTop (&s1,&e);
           if (e== ' {') Pop (&s1,&e);
          else flag=0;
        Break
      } ++i;
    } if (Empty (&AMP;S1) &&flag) return true;
  else return false;
  test.c #include "Stack.h" int main (void) {char str[]= "[20* (16+4)/(15-6)]"; if (MyFunc (str)) printf ("Match succeeded.")
  \ n "); else printf ("Match failed.")
  \ n ");
  System ("pause");
  return 0;
 }

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.