#include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 typedef char ElemType; typedef struct StackNode { ElemType data; struct StackNode *next; }StackNode,*LinkStack; void InitStack(LinkStack *p) { LinkStack q=(LinkStack)malloc(sizeof(StackNode)); q->next=NULL; *p=q; } int Push(LinkStack top,ElemType e) { LinkStack s; s=(LinkStack)malloc(sizeof(StackNode)); if(s==NULL) return FALSE; s->data=e; s->next=top->next; top->next=s; return TRUE; } int GetTop(LinkStack s,ElemType *e) { if(s->next==NULL) return FALSE; else { *e=s->next->data; return TRUE; } } int Match(char ch1,char ch2) { if((ch1=='('&&ch2==')')|| (ch1=='['&&ch2==']')|| (ch1=='{'&&ch2=='}')) return TRUE; else return FALSE; } void BracketMatch(char *str) { LinkStack S;int i;char ch; InitStack(&S); for(i=0;str[i]!='\0';i++) { switch(str[i]) { case'(': case'[': case'{': Push(S,str[i]); break; case')': case']': case'}': if(IsEmpty(S)) {printf("2"); return; } else { printf("3"); return; } } if(IsEmpty(S)) printf("4"); else printf("1"); } }
全部评论
(1) 回帖