看数据结构写代码(11)栈的应用(二) 括号匹配的检查

下面代码 可以检查 “() []” 括号不匹配的问题。

这个代码 是在 第9篇 linkStack 的基础上开发的。 在开发中,,遇到了3个问题。

一是发现了 linkStack 在 clearStack 时 没有将头指针的后继指向NULL问题。

二是 对 char * a 与 char a[] 认识不够清楚

三是 对 scanf 认识不够清楚 , 当 输入 加入一些空格时,造成 我 不能 理解的结果.

下面上代码:

// LinkStack.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>typedef char elelmentType;enum E_State{E_State_Error = 0,E_State_Ok = 1,};//链表节点nodestruct lStackNode{elelmentType data;lStackNode * next;};//链栈struct linkStack{lStackNode * bottom;lStackNode * top;int len;};lStackNode * makeNode(elelmentType data){lStackNode * pNode = (lStackNode *)malloc(sizeof(lStackNode));if (pNode != NULL){pNode->data = data;pNode->next = NULL;}return pNode;}E_State stackInit(linkStack * lStack){//分配头节点lStackNode * pNode = (lStackNode *) malloc(sizeof(lStackNode));if (pNode == NULL){return E_State_Error;}pNode->next = NULL;//栈顶和栈底指向同一个节点时为空.lStack->bottom = lStack->top = pNode;lStack->len = 0;return E_State_Ok;}void stackClear(linkStack * stack){lStackNode * next = stack->bottom->next;while (next != NULL){lStackNode * freeNode = next;/*又粗心了。。。free(freeNode);next = next->next;*/next = next->next;free(freeNode);}stack->top = stack->bottom;//忘记 了//错误stack->bottom->next = NULL;stack->len = 0;}void stackDestory(linkStack * stack){stackClear(stack);free(stack->top);stack->top = stack->bottom = NULL;}E_State stackGetTop(linkStack stack,elelmentType * topData){//链表的栈顶 指向 栈顶元素if (stack.top != stack.bottom){*topData = stack.top->data;return E_State_Ok;}else{return E_State_Error;}}int stackLen(linkStack stack){return stack.len;}bool stackEmpty(linkStack stack){return stack.top == stack.bottom ? true : false;}E_State stackPush(linkStack * stack,elelmentType data){lStackNode * node = makeNode(data);if (node != NULL){stack->top->next = node;stack->top = node;stack->len++;}else{return E_State_Error;}}E_State stackPop(linkStack * stack,elelmentType * data){if (stack->top != stack->bottom){//首先指向第一个元素.lStackNode * next = stack->bottom;*data = stack->top->data;//找到栈顶元素的前驱while (next->next != stack->top){next = next->next;}free(stack->top);next->next = NULL;stack->top = next;//忘记加了stack->len–;return E_State_Ok;}else{return E_State_Error;}}//从栈底到 栈顶的 遍历void stackTraverse(linkStack stack){//首先指向第一个元素lStackNode * next = stack.bottom->next;printf("————遍历开始———-\n");while (next != NULL){printf("———–%c———-\n",next->data);next = next->next;}printf("————遍历结束———-\n");}//括号匹配#include <cstring>enum E_Kind{E_Kind_Other = 0,//其他E_Kind_Left,//左括号 ([E_Kind_Right,//右括号 )]};E_Kind getCharKind(char c){if (c == '(' || c == '['){return E_Kind_Left;}else if(c == ')' || c == ']'){return E_Kind_Right;}else{return E_Kind_Other;}}int _tmain(int argc, _TCHAR* argv[]){linkStack stack;stackInit(&stack);while (true){//错误 char * s = "sdfdfsdfssdfsd";char s[50];printf("请输入表达式:");scanf("%s",s);printf("表达式为:%s\n",s);if (strcmp(s,"exit") == 0){break;}char * p = s;while (*p != '\0'){char data = *p;E_Kind kind = getCharKind(data);if (kind == E_Kind_Left){stackPush(&stack,data);}else if(kind == E_Kind_Right){char top;//错误//top = stackGetTop(stack,&top);stackGetTop(stack,&top);if ((data == ')' && top == '(') || (data == ']' && top == '[')){stackPop(&stack,&top);}else{//匹配异常break;}}p++;}if (stackEmpty(stack)){printf("表达式匹配正确\n");}else{printf("表达式匹配异常\n");}//清空栈printf("\n");stackClear(&stack);}return 0;}

运行截图:

错误3错误示例:

心有多大,舞台就有多大。

看数据结构写代码(11)栈的应用(二) 括号匹配的检查

相关文章:

你感兴趣的文章:

标签云: