栈的简单应用(括号匹配、逆波兰表达式)

顺序栈的基本操作
链式栈的基本操作

括号匹配

//SqStack.c

int IsBracket(char ch)
{
    if (('}' == ch) || ('{'== ch)||
        (']' == ch) || ('[' ==ch)||
        (')' == ch) || ('('==ch))
    {
        return 1;
    }

    return 0;
}


void MatchBracket(const char * pStr)
{
    int i = 0;
    int size = strlen(pStr);
    SqStack s;
    SqStackInit(&s);

    for (i = 0; i < size; ++i)
    {
        if (!IsBracket(pStr[i]))
        {
            continue;
        }
        else
        {
            if ('{' == pStr[i] || '(' == pStr[i] || '[' == pStr[i])
            {
                SqStackPush(&s, pStr[i]);
            }
            else
            {
                if (SqStackEmpty(&s))
                {
                    printf("右括号比左括号多!\n");
                    return;
                }
                else
                {   
                    char c = StackTop(&s);
                    if ((')' == pStr[i] && '(' == c)||
                        (']' == pStr[i] && '[' == c)||
                        ('}' == pStr[i] && '{' == c))
                    {
                        SqStackPop(&s);
                    }
                    else
                    {
                        printf("左右括号不匹配!\n");
                        return;
                    }
                }
            }
        }
    }

    if (SqStackEmpty(&s))
    {
        printf("左右括号匹配!\n");
    }
    else
    {
        printf("左括号比右括号多!\n");
    }
}

void TestBracket()
{

    char a[] = "}))abc{[(])}";
    char b[] = "(())))abc{[]}";
    char c[] = "(()()abc{[]}";
    char d[] = "(())abc{()}";
    MatchBracket(a);
    MatchBracket(b);
    MatchBracket(c);
    MatchBracket(d);

}
//SqStack.h

void MatchBracket(const char * pStr);           
int IsBracket(char ch);
void TestBracket();

逆波兰表达式

//SqList.c

int  ExpRPN(Cell * RPN, int size)
{
    int i = 0;
    SqStack s;
    SqStackInit(&s);
    for (i = 0; i < size; ++i)
    {
        if (RPN[i]._op == DATA)
        {
            SqStackPush(&s, RPN[i]._data);
        }

        else
        {
            int left = 0;
            int right = 0;
            right = (StackTop(&s));
            SqStackPop(&s);
            left = (StackTop(&s));
            SqStackPop(&s);
            switch (RPN[i]._op)
            {
            case ADD:
                SqStackPush(&s, left + right);
                break;
            case SUB:
                SqStackPush(&s, left - right);
                break;
            case DIV:
                if (0 == left)
                    assert(0);
                else
                {
                    SqStackPush(&s, left / right);
                }
                break;
            case MUL:
                SqStackPush(&s, left*right);
                break;
            default:
                break;
            }
        }

    }

    return  StackTop(&s);
}


void TestRPN()
{
    Cell RPN[] = { { DATA, 12 }, { DATA, 3 }, { DATA, 4 }, { ADD, 0 },
                    { MUL, 0 }, { DATA, 6 }, { SUB, 0 }, { DATA, 8 }, 
                    { DATA, 2 }, { DIV, 0 }, { ADD, 0 } };

    int size = sizeof(RPN) / sizeof(RPN[0]);
    int ret = ExpRPN(RPN, size);
    printf("ret = %d\n", ret);

}

//SqStack.h


enum OPERATOR{ ADD, SUB, MUL, DIV, DATA };

typedef struct Cell
{
    enum OPERATOR _op;
    int _data;

}Cell;

void TestRPN();

你可能感兴趣的:(数据结构)