当前位置:网站首页>Application of stack -- bracket matching problem

Application of stack -- bracket matching problem

2022-07-01 12:23:00 Between the steps

The application of the stack —— Bracket matching problem

#include<stdio.h>

#define MaxSize 10
typedef struct{
    
char data[MaxSize];    // Static arrays hold the elements in the stack 
int top;              // Top pointer of stack 
}SqStack;

// Initialization stack 
void InitStack(SqStack &S){
    }
// Judge whether the stack is empty 
bool StackEmpty(SqStack s){
    }
// Put new elements on the stack 
bool Push(SqStack &s,char x){
    }

// Stack top element out of stack , use x return 
bool Pop(SqStack &s, char &x){
    }

bool braketCheak(char str[],int length){
       // Put characters in str[] Array ,length  It's the length 
    SqStack S;    // Create a stack and allocate space for it 
    InitStack(S);  
    for(int i=0;i<length;i++){
    
        if(str[i]=='(' ||str[i]=='['||str[i]=='{'){
       
            
            Push(S,str[i]);                            // Left bracket in stack 
           }else{
                                           
           if(StackEmpty(S))                          // If it's not the left parenthesis , It's time to get out of the stack , If the stack is empty , be false
                return false; 
           
           char topElem;                      // Out of stack elements 
           Pop(S,topElem); // Stack top element out of stack 
           if(str[i]==')'&& topElem!='(')
                return false;
           if(str[i]==']'&& topElem!='[')
                return false;
            if(str[i]=='}'&& topElem!='{')
                return false;
    }
    }
    return StackEmpty(S);  // If the stack is empty, return true, Otherwise return to false
}

int main(){
    

}
原网站

版权声明
本文为[Between the steps]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011204421613.html