summaryrefslogtreecommitdiff
path: root/c/dataStructure/栈和队列//链栈/p_match.c
blob: 44efecdf344ff087119441b372de8e69082b19e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stack.h"

int main(void)
{
    ElemType ch;
    Stack s;
    ElemType pop, top;
    bool flag = true;

    InitStack(&s);

    while ((ch = getchar()) != '\n' && flag)
    {
        switch (ch)
        {
            case '(':   Push(&s,ch);
                        break;
            case '[':   Push(&s,ch);
                        break;
            case ')':   if (StackIsEmpty(&s) || (top = GetTop(&s)) != '(')
                            flag = false;
                        else if (top == '(')
                            Pop(&s,&pop);
                        break;
            case ']':   if (StackIsEmpty(&s) || (top = GetTop(&s)) != '[')
                            flag = false;
                        else if (top == '[')
                            Pop(&s,&pop);
                        break;
        }
    }

    if (StackIsEmpty(&s) && flag)
        printf("Matching successful!\n\n");
    else
    {
        fprintf(stderr,"Matching failed!\n\n");
        DestroyStack(&s);
    }
    return 0;
}