关于栈的使用之括号匹配
数据结构练习题
一、括号匹配(栈的使用)
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