算法学习 - 括号匹配(栈实现)C++
括号匹配是栈最典型的应用了。
其实思路很简单,就是遇到一个左括号就压栈,遇到一个右括号就弹栈,看是否匹配就好了。最后检查下栈里是不是有剩余的括号就行了。
上代码~:
// // main.cpp // bracketMatch // // Created by Alps on 14-7-28. // Copyright (c) 2014年 chen. All rights reserved. // #include <iostream> #define ElementType char using namespace std; struct Node; typedef Node * PtrToNode; typedef PtrToNode Stack; struct Node{ ElementType X; PtrToNode Next; }; int isEmpty(Stack S){ return S->Next == NULL; } Stack createStack(void){ Stack S; S = (Stack)malloc(sizeof(Stack)); S->Next = NULL; return S; } void Push(Stack S, ElementType X){ Stack element; element = (Stack)malloc(sizeof(Stack)); element->X = X; element->Next = S->Next; S->Next = element; } void Pop(Stack S){ if (!isEmpty(S)) { Stack tmp = S->Next; S->Next = tmp->Next; free(tmp); }else{ printf("Empty stack can't pop cell\n"); } } ElementType Top(Stack S){ ElementType X; if (!isEmpty(S)) { return X = S->Next->X; }else{ printf("empty stack, no top cell\n"); } return 0; } void makeEmpty(Stack S){ if (S == NULL) { printf("create stack first\n"); } while (!isEmpty(S)) { Pop(S); } } int main(int argc, const char * argv[]) { Stack S = createStack(); int i = 0; char tmp; int flag = 0; char A[] = {'(','b','c',')','[','{','a','(',')','c','}',']'}; for (i = 0; i < sizeof(A)/sizeof(char); i++) { if (A[i] == '(' || A[i] == '[' || A[i] == '{') { Push(S, A[i]); continue; }else{ if (A[i] == ')' || A[i] == ']' || A[i] == '}') { tmp = Top(S); switch (A[i]) { case ')': if (tmp == '(') { flag = 1; } break; case ']': if (tmp == '[') { flag = 1; } break; case '}': if (tmp == '{') { flag = 1; } break; default: break; } if (flag != 1) { printf("the bracket doesn't match! %c-%d\n",A[i],i); return 1; }else{ Pop(S); flag = 0; } }else{ continue; } } } if (!isEmpty(S)) { printf("there are already some barackets not matched!\n"); return 1; }else{ printf("the bracket is matched\n"); } return 0; }
其实主要流程都在main函数里,我没有变成函数。。。多包涵好了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。