C语言用栈实现括号匹配问题

例如:{}[()]、{[()]}、()[]{}这种大中小括号成对出现(位置不限)则为括号匹配,反之则不匹配,如{()[

接下来看一下实现方式


栈的定义以及相关操作
//栈的定义
typedef struct{
	char elem[stack_size];
	int top;
}seqStack;
//栈的初始化
void initStack(seqStack *s){
	s->top=-1;
}
//判断栈空
int isEmpty(seqStack *s){
	if(s->top==-1)
		return 1;
	else
		return 0;
}
//入栈
int push(seqStack *s,char c){
	if(s->top==stack_size-1)
		return 0;
	else{
		s->top++;
		s->elem[s->top]=c;
		return 1;
	}
}
//出栈
int pop(seqStack *s,char *x){
	if(s->top==-1)
		return 0;
	else{
		*x=s->elem[s->top];
		s->top--;
		return 1;
	}

}

//获取栈顶元素
int gettop(seqStack *s,char *x){
	if(!isEmpty(s)){
		*x=s->elem[s->top];
		return 1;
	}
	else
		return 0;
}
括号处理

括号匹配的思想:依次从左至右检查字符串,若为左括号,则入栈,若遇右括号则获取栈顶元素,检查栈顶元素与当前元素是否匹配,若匹配,则栈顶元素出栈。反之,则不匹配,程序结束。
以此类推,直至检查完所有字符串。如果此时栈空则匹配,反之则不匹配。

//成对的左右括号的ASCII码相差1或者2,以此结论来判断左右括号是否成对出现
int match(char a,char b){
	if(a+1==b||a+2==b)//成对的左右括号的ASCII码相差1或者2
		return 1;
	return 0;
}

int bracketMarch(char a[]){
	seqStack s;
	char ch;
	int i;
	initStack(&s);
	for(i=0;a[i]!='\0';i++){
		switch(a[i]){
		case '{':
		case '[':
		case '(':
			push(&s,a[i]);//遇到左括号则入栈
			break;
		case '}':
		case ']':
		case ')':
			if(isEmpty(&s)){
				return 0;
			}
			else{
			gettop(&s,&ch);//遇到右括号获取栈顶元素
			if(match(ch,a[i]))//检查是否匹配
				pop(&s,&ch);//若匹配则栈顶元素出栈
			else
				return 0;
			}
		}
	}
	if(isEmpty(&s))//如果栈空,则括号是匹配的
		return 1;
	else//反之,则不匹配
		return 0;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define stack_size 100
//有关栈的操作

//栈的定义
typedef struct{
	char elem[stack_size];
	int top;
}seqStack;
//栈的初始化
void initStack(seqStack *s){
	s->top=-1;
}
//判断栈空
int isEmpty(seqStack *s){
	if(s->top==-1)
		return 1;
	else
		return 0;
}
//入栈
int push(seqStack *s,char c){
	if(s->top==stack_size-1)
		return 0;
	else{
		s->top++;
		s->elem[s->top]=c;
		return 1;
	}
}
//出栈
int pop(seqStack *s,char *x){
	if(s->top==-1)
		return 0;
	else{
		*x=s->elem[s->top];
		s->top--;
		return 1;
	}

}

//获取栈顶元素
int gettop(seqStack *s,char *x){
	if(!isEmpty(s)){
		*x=s->elem[s->top];
		return 1;
	}
	else
		return 0;
}
int match(char a,char b){
	if(a+1==b||a+2==b)//成对的左右括号的ASCII码相差1或者2
		return 1;
	return 0;
}
int bracketMarch(char a[]){
	seqStack s;
	char ch;
	int i;
	initStack(&s);
	for(i=0;a[i]!='\0';i++){
		switch(a[i]){
		case '{':
		case '[':
		case '(':
			push(&s,a[i]);//遇到左括号则入栈
			break;
		case '}':
		case ']':
		case ')':
			if(isEmpty(&s)){
				return 0;
			}
			else{
			gettop(&s,&ch);//遇到右括号获取栈顶元素
			if(match(ch,a[i]))//检查是否匹配
				pop(&s,&ch);//若匹配则栈顶元素出栈
			else
				return 0;
			}
		}
	}
	if(isEmpty(&s))//如果栈空,则括号是匹配的
		return 1;
	else//反之,则不匹配
		return 0;
}
int main(){
	int n;
	char a[25];
	scanf("%d",&n);
	while(n--){//你想检查几个字符串是否括号匹配?
		scanf("%s",a);
		if(bracketMarch(a))
			printf("Yes\n");
		else
			printf("No\n");
	}
}

ps:如有问题欢迎留言-

  • 58
    点赞
  • 221
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值