赞
踩
括号匹配问题是较为经典的栈的应用了,在Leetcode和数据结构书中时常出现
给定一个字符串,其包含( , ) ,{ , } , [ , ]以及数字,要求判断其括号是否匹配,即:
实例 1:
输入: "[2 + (1 - 3)] * 4"
输出:1
实例 2:
输入:"( ) )"
输出:0
typedef struct CharStack { int top; /* 用于栈顶指针 */ int data[MAXSIZE]; } *CharStackPtr,CharStack; //输出栈 void outPutStack(CharStackPtr tempStack) { for(int i = 0; i <= tempStack->top; ++i) { printf("%c ",tempStack->data[i]); } printf("\n"); } //初始化栈 CharStackPtr CharStackInit() { CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack)); resultPtr->top = -1; return resultPtr; } //压栈 void push(CharStackPtr tempStackPtr, char tempValue) { if(tempStackPtr->top >= MAXSIZE - 1) { printf("无法压入元素%c:堆栈已满!\n",tempValue); return ; } tempStackPtr->top ++; tempStackPtr->data[tempStackPtr->top] = tempValue; } //弹栈 char pop(CharStackPtr tempStackPtr) { if (tempStackPtr->top == -1) { printf("不能弹出元素:堆栈为空!\n"); return NULL; } tempStackPtr->top --; return tempStackPtr->data[tempStackPtr->top+1]; }
//括号匹配 bool parenthesisMatching(char* tempString) { /* 在栈底部压入'#'并初始化堆栈。 */ CharStackPtr tempStack = CharStackInit(); push(tempStack,'#'); char tempChar,tempPopedChar; for(int i = 0; i< strlen(tempString); ++i) { tempChar = tempString[i]; switch (tempChar) { case '(': case '[': case '{': push(tempStack,tempChar); break; case ')': tempPopedChar = pop(tempStack); if(tempPopedChar != '(') { return false; } break; case ']': tempPopedChar = pop(tempStack); if(tempPopedChar != '[') { return false; } break; case '}': tempPopedChar = pop(tempStack); if(tempPopedChar != '{') { return false; } break; default: break; } } tempPopedChar = pop(tempStack); if(tempPopedChar != '#') { return false; } return true; }
#include <stdio.h> #include <string.h> #include <malloc.h> #define MAXSIZE 10 typedef struct CharStack { int top; /* 用于栈顶指针 */ int data[MAXSIZE]; } *CharStackPtr,CharStack; //输出栈 void outPutStack(CharStackPtr tempStack) { for(int i = 0; i <= tempStack->top; ++i) { printf("%c ",tempStack->data[i]); } printf("\n"); } //初始化栈 CharStackPtr CharStackInit() { CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack)); resultPtr->top = -1; return resultPtr; } //压栈 void push(CharStackPtr tempStackPtr, char tempValue) { if(tempStackPtr->top >= MAXSIZE - 1) { printf("无法压入元素%c:堆栈已满!\n",tempValue); return ; } tempStackPtr->top ++; tempStackPtr->data[tempStackPtr->top] = tempValue; } //弹栈 char pop(CharStackPtr tempStackPtr) { if (tempStackPtr->top == -1) { printf("不能弹出元素:堆栈为空!\n"); return NULL; } tempStackPtr->top --; return tempStackPtr->data[tempStackPtr->top+1]; } //括号匹配 bool parenthesisMatching(char* tempString) { /* 在栈底部压入'#'并初始化堆栈。 */ CharStackPtr tempStack = CharStackInit(); push(tempStack,'#'); char tempChar,tempPopedChar; for(int i = 0; i< strlen(tempString); ++i) { tempChar = tempString[i]; switch (tempChar) { case '(': case '[': case '{': push(tempStack,tempChar); break; case ')': tempPopedChar = pop(tempStack); if(tempPopedChar != '(') { return false; } break; case ']': tempPopedChar = pop(tempStack); if(tempPopedChar != '[') { return false; } break; case '}': tempPopedChar = pop(tempStack); if(tempPopedChar != '{') { return false; } break; default: break; } } tempPopedChar = pop(tempStack); if(tempPopedChar != '#') { return false; } return true; } //测试 void parenthesisMatchingTest() { char *tempExpression; tempExpression = "[2 + (1 - 3)] * 4"; bool tempMatch = parenthesisMatching(tempExpression); printf("表达式%s括号匹配吗? %d\n",tempExpression,tempMatch); tempExpression = "( ) )"; tempMatch = parenthesisMatching(tempExpression); printf("表达式%s括号匹配吗? %d\n",tempExpression,tempMatch); tempExpression = "()()(())"; tempMatch = parenthesisMatching(tempExpression); printf("表达式%s括号匹配吗? %d\n",tempExpression,tempMatch); tempExpression = "({}[])"; tempMatch = parenthesisMatching(tempExpression); printf("表达式%s括号匹配吗? %d\n",tempExpression,tempMatch); tempExpression = ")("; tempMatch = parenthesisMatching(tempExpression); printf("表达式%s括号匹配吗? %d\n",tempExpression,tempMatch); } int main() { parenthesisMatchingTest(); }
表达式[2 + (1 - 3)] * 4括号匹配吗? 1
表达式( ) )括号匹配吗? 0
表达式()()(())括号匹配吗? 1
表达式({}[])括号匹配吗? 1
表达式)(括号匹配吗? 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。