博客
关于我
括号匹配问题
阅读量: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 多进程-进阶-进程间通信之Pipe
查看>>
python 多进程-进阶-进程间通信之Queue
查看>>
python 如何“否定“value : 如果为真则返回假,如果为假则返回真
查看>>
Python 如何在 Web 环境中使用 Matplotlib 进行数据可视化
查看>>
python 字符串中特定字符替换,截取
查看>>
Python 字符串总结
查看>>
python 字符串显示中文_Python字符串开头的b"、u"、r"与中文乱码
查看>>
Python 字符串的几种拼装方式
查看>>
python 存入数据库bigint_python基础_MySQL的bigint类型
查看>>
python 学习 面向对象编程
查看>>
Python 学习日记第三篇 -- 字典
查看>>
Python 学习笔记(一)Data type
查看>>
python 安装echarts
查看>>
python 安装opencv及问题解决
查看>>
Python 安装程序:“写入文件 C:\Python27\pythonw.exe 时出错“
查看>>
Python 安装避坑指南:从入门到进阶
查看>>
Python 安装配置详解
查看>>
python 实现儿童算术游戏
查看>>