博客
关于我
括号匹配问题
阅读量:229 次
发布时间:2019-03-01

本文共 1774 字,大约阅读时间需要 5 分钟。

typedef char DataType;typedef struct Stack {    DataType* arr;    int size;    int capacity;} Stack;//初始化void StackInit(Stack* st, int n) {    st->size = 0;    st->capacity = n;    st->arr = (DataType*)malloc(sizeof(DataType) * n);}//进栈void StackPush(Stack* st, DataType data) {    if (st->size >= st->capacity) {        st->capacity += st->capacity;        st->arr = (DataType*)realloc(st->arr, st->capacity * sizeof(DataType));    }    st->arr[st->size++] = data;}//出栈void StackPop(Stack* st) {    if (st->size == 0) {        return;    }    st->size--;}//得到栈顶元素DataType getStackTop(Stack* st) {    return st->arr[st->size - 1];}//得到栈的大小int getStackSize(Stack* st) {    return st->size;}//判空int StackEmpty(Stack* st) {    return st->size == 0 ? 1 : 0;}//销毁void StackDestroy(Stack* st) {    free(st->arr);    st->arr = NULL;    st->capacity = 0;    st->size = 0;}//字符串有效性检查bool isValid(char* s) {    Stack st;    StackInit(&st, 10);    char map[3][2] = { {'(', ')'}, {'[', ']'}, {'{', '}' } };    while (*s != '\0') {        int foundLeft = 0;        for (int i = 0; i < 3; i++) {            if (*s == map[i][0]) {                foundLeft = 1;                StackPush(&st, *s);                s++;                break;            }        }        if (!foundLeft) {            if (StackEmpty(&st)) {                return false;            }            for (int j = 0; j < 3; j++) {                if (*s == map[j][1]) {                    char top = getStackTop(&st);                    if (top == map[j][0]) {                        StackPop(&st);                        s++;                        break;                    } else {                        return false;                    }                }            }        }    }    return StackEmpty(&st) == 1;}

转载地址:http://uunv.baihongyu.com/

你可能感兴趣的文章
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>
Python-numpy(2)数组的操作
查看>>
Python-Numpy的tile函数用法
查看>>
python-open函数
查看>>
Python-Pandas-将特定函数应用于给定级别-多索引数据框架
查看>>
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>