括号匹配【利用栈实现】【数据结构】

括号匹配

1.简述

利用栈的思想来实现括号匹配,假设给你一个"{a+c[(a+b)/d]}",判断括号之间是否两两匹配

算法思想:我们可以利用栈来实现,遍历整个字符串,遇到 [ 或 { 或( 就入栈,遇到右括号就出栈 栈顶元素,

判断出栈元素是否和对应右括号匹配,不匹配则匹配失败,匹配成功则接着遍历,遇到右括号则接着

取栈顶元素判断

2.图示

在这里插入图片描述

3.代码部分
3.1 栈代码

括号匹配的核心就是利用了栈后进先出的特点,这里我们实现了对栈的一系列的操作,

方便我们后续调用函数,实现起来更加方便

/**
  * 括号匹配的实现
  * @Date 2024-08-11 10:48
  */
#include <stdio.h>

#define Max 20
typedef struct Stack{
    char data[Max];
    int top;//栈顶指针
} Stack;
//初始化栈
void InitStack(Stack &stack){
    stack.top = -1;
}
//判断栈空
bool isEmpty(Stack &stack){
    if(stack.top == -1){
        printf("the stack is empty\n");
        return true;
    }
    return false;
}
//判断栈满
bool isFull(Stack &stack){
    if (stack.top == Max - 1){
        printf("the stack is full");
        return true;
    }
    return false;
}
//入栈
bool Push(Stack &stack,char x){
    //判断栈是否满
    if (isFull(stack)){
        return false;
    }
    stack.data[++stack.top] = x;
    return true;
}
//出栈
bool Pop(Stack &stack){
    //判断栈是否为空
    if (isEmpty(stack)){
        return false;
    }
    stack.top--;
    return true;
}
//获取栈顶元素(只是获取栈顶元素,不必传入引用类型,但是x是带回元素,需用引用传递)
void getTop(Stack stack,char &x){
    //判断栈是否为空
    if (isEmpty(stack)){
        return;
    }
    x=stack.data[stack.top];
}
3.2 括号匹配的核心代码
//括号匹配的逻辑
bool isMatch(char A[],Stack &s){
    for (int i = 0; A[i]!='\0'; i++) {
        if (A[i]=='{'||A[i]=='['||A[i]=='('){
            //遇到左括号入栈
            Push(s,A[i]);
        } else if (A[i]=='}'||A[i]==']'||A[i]==')'){
            char x;//取出栈顶元素
            getTop(s,x);
            Pop(s);//出栈
            //如果是右括号则获取栈顶元素,看是否与右括号匹配
            //三种情况不匹配,直接返回false
            if (A[i]=='}' && x!='{'){
                printf("no match {}\n");
                return false;
            }
            if(A[i]==']' && x!='['){
                printf("no match []\n");
                return false;
            }
            if(A[i]==')' && x!='('){
                printf("no match ()\n");
                return false;
            }
        }
    }
    //如果此时栈不为空,证明有没有匹配的括号,匹配失败
    if (s.top != -1){
        printf("the stack not empty\n");
        return false;
    }
    return true;
}
3.3 代码测试
int main(){
    Stack S;
    InitStack(S);

    //准备表达式
    char A[] = "{a+[(v+d)/d]+d}";
    if (isMatch(A,S)){
        printf("match success!!!");
    } else{
        printf("fail!!!");
    }

    return 0;
}
3.4 测试结果

在这里插入图片描述

4.完整代码
/**
  * 括号匹配的实现
  * @Date 2024-08-11 10:48
  */
#include <stdio.h>

#define Max 20
typedef struct Stack{
    char data[Max];
    int top;//栈顶指针
} Stack;
//初始化栈
void InitStack(Stack &stack){
    stack.top = -1;
}
//判断栈空
bool isEmpty(Stack &stack){
    if(stack.top == -1){
        printf("the stack is empty\n");
        return true;
    }
    return false;
}
//判断栈满
bool isFull(Stack &stack){
    if (stack.top == Max - 1){
        printf("the stack is full");
        return true;
    }
    return false;
}
//入栈
bool Push(Stack &stack,char x){
    //判断栈是否满
    if (isFull(stack)){
        return false;
    }
    stack.data[++stack.top] = x;
    return true;
}
//出栈
bool Pop(Stack &stack){
    //判断栈是否为空
    if (isEmpty(stack)){
        return false;
    }
    stack.top--;
    return true;
}
//获取栈顶元素(只是获取栈顶元素,不必传入引用类型,但是x是带回元素,需用引用传递)
void getTop(Stack stack,char &x){
    //判断栈是否为空
    if (isEmpty(stack)){
        return;
    }
    x=stack.data[stack.top];
}
//括号匹配的逻辑
bool isMatch(char A[],Stack &s){
    for (int i = 0; A[i]!='\0'; i++) {
        if (A[i]=='{'||A[i]=='['||A[i]=='('){
            //遇到左括号入栈
            Push(s,A[i]);
        } else if (A[i]=='}'||A[i]==']'||A[i]==')'){
            char x;//取出栈顶元素
            getTop(s,x);
            //如果不是说明匹配成功,括号出栈
            Pop(s);
            //如果是右括号则获取栈顶元素,看是否与右括号匹配
            //三种情况不匹配,直接返回false
            if (A[i]=='}' && x!='{'){
                printf("no match {}\n");
                return false;
            }
            if(A[i]==']' && x!='['){
                printf("no match []\n");
                return false;
            }
            if(A[i]==')' && x!='('){
                printf("no match ()\n");
                return false;
            }
        }
    }
    //如果此时栈不为空,证明有没有匹配的括号,匹配失败
    if (s.top != -1){
        printf("the stack not empty\n");
        return false;
    }
    return true;
}
int main(){
    Stack S;
    InitStack(S);

    //准备表达式
    char A[] = "{a+[(v+d)/d]+d}";
    if (isMatch(A,S)){
        printf("match success!!!");
    } else{
        printf("fail!!!");
    }

    return 0;
}

有错误还请大佬指正啊!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值