關於棧的使用之括號匹配
阿新 • • 發佈:2021-10-15
資料結構練習題
一、括號匹配(棧的使用)
1.順序棧實現
#include "stdio.h" #define MaxSize 100 typedef char ElemType; struct Stack{ ElemType data[MaxSize]; int top; }; //初始化棧 void InitStack(struct Stack* S){ S->top=-1; } //棧判空 int EmptyStack(struct Stack* S){ if(S->top==-1){ return 1; }else{ return 0; } } //插入一個元素 int Push(struct Stack* S,ElemType x){ if(S->top==MaxSize-1) return 0; S->top++; S->data[S->top]=x; return 1; } //刪除一個元素 int Pop(struct Stack* S,ElemType* x){ if(S->top==-1) return 0;//判空 *x=S->data[S->top]; S->top--; return 1; } //獲取棧頂元素 int GetHead(struct Stack* S,ElemType* x){ if(S->top==-1)return 0; *x=S->data[S->top]; return 1; } //處理括號匹配 int Matching(char str[],int length){ int i; char compare; struct Stack S; InitStack(&S); for(i=0;i<length;i++){ if(str[i]=='('||str[i]=='['||str[i]=='{'){ Push(&S,str[i]); }else{ if(EmptyStack(&S)){ return 0; }else{ Pop(&S,&compare);//取出棧頂元素,並存儲在compare中 if(str[i]==')'&&compare!='(') return 0; if(str[i]==']'&&compare!='[') return 0; if(str[i]=='}'&&compare!='}') return 0; } } } return EmptyStack(&S); } int LengthToStr(const char* str){ int i=0; while (str[i]!='\n'){ i++; } return i; } int main() { char string[]="[[][()()]]\n"; int length= LengthToStr(string); if(Matching(string,length)){ printf("The bracket-match-problem is success!\n"); }else{ printf("The bracket-match-problem is fail!\n"); } return 0; }
實現結果:
D:\project\clion\ch3\cmake-build-debug\parenthesis_matching.exe
The bracket-match-problem is success!
Process finished with exit code 0
2.鏈棧實現
#include "stdio.h" #include "stdlib.h" typedef char ElemType; struct Node{ ElemType data; struct Node* next; }; //建立一個節點 struct Node* CreateStack(ElemType x){ struct Node* p= (struct Node*)malloc(sizeof(struct Node)); if(!p){ printf("No enough memory to allocate!\n"); exit(0); } p->data=x; p->next=NULL; return p; } //初始化棧 void InitStack(struct Node* S){ S=NULL; } //棧的判空 int EmptyStack(struct Node* S){ return S==NULL; } //插入一個元素 struct Node* Push(struct Node* S,ElemType x){ struct Node* s= CreateStack(x);//建立一個節點 if(!EmptyStack(S)){//當前棧為空 s->next=S; } S=s; return S; } //刪除一個元素 struct Node* Pop(struct Node* S,ElemType* x){ struct Node* p=S; if(EmptyStack(S)) return NULL; *x=S->data; S=p->next; free(p); return S; } //獲取棧頂元素 int GetTop(struct Node* S,ElemType* x){ if(EmptyStack(S))return 0; *x=S->data; return 1; } //處理括號匹配 int Matching(char str[],int length){ int i; char compare; struct Node* S=NULL; InitStack(S); for(i=0;i<length;i++){ if(str[i]=='('||str[i]=='['||str[i]=='{'){ S=Push(S,str[i]); }else{ if(EmptyStack(S)){ return 0; }else{ S=Pop(S,&compare);//取出棧頂元素,並存儲在compare中 if(str[i]==')'&&compare!='(') return 0; if(str[i]==']'&&compare!='[') return 0; if(str[i]=='}'&&compare!='}') return 0; } } } return EmptyStack(S); } int LengthToStr(const char* str){ int i=0; while (str[i]!='\n'){ i++; } return i; } int main(){ char string[]="[[[]()()]]\n"; int length= LengthToStr(string); if(Matching(string,length)){ printf("The bracket-match-problem is success!\n"); }else{ printf("The bracket-match-problem is fail!\n"); } return 0; }
實現結果
D:\project\clion\ch3\cmake-build-debug\parenthesis_matching_list.exe
The bracket-match-problem is success!
Process finished with exit code 0