Bracket matching test data structure [C language]

  "Data Structure (C Language Edition)" Yan Weimin's case
  Original question: Assume that two kinds of brackets are allowed in the expression: parentheses and square brackets, and the nesting method is arbitrary, that is, ([]()) etc. are all correct formats, 【( 】) is an incorrect format. Design an algorithm to check whether the parentheses in the input string are matched.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define STACK_INIT_SIZE 10 // 初始化分配量
#define STACKINCREMENT 1  // 分配增量
#define ERROR 0
#define OK 1

typedef int Status;
typedef char SElemType;

typedef struct{
    
    
	SElemType *base;  // 栈底指针
    SElemType *top;   // 栈顶元素
    int stacksize;    //栈的大小 
}SqStack;

//初始化
Status InitStack(SqStack &S){
    
    
	S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	if(!S.base) 
	return ERROR;
	S.top=S.base;
	S.stacksize=STACK_INIT_SIZE;
	return OK;
} 

//判断是否是空栈
Status StackEmpty(SqStack &S){
    
    
	if(S.top==S.base) return OK;
	else return ERROR;
}

Status StackLength(SqStack &S)
{
    
       // 获取栈容量
    return S.top - S.base;
}

// 入栈 
Status Push(SqStack &S, SElemType *e)
{
    
    
    if(S.top - S.base == S.stacksize)
    {
    
    
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if(!S.base) return ERROR;
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top ++= *e;
    return OK;
}

//出栈
 Status Pop(SqStack &S, SElemType *e)
{
    
     
    if(S.top == S.base) return ERROR;
    *e = *--S.top;
    return OK;
}


// 获取栈顶元素
Status GetTop(SqStack &S,SElemType *e){
    
    
//	如果不是空栈,用e返回S的栈顶元素,并返回OK,否则返回ERROR
   if(S.top==S.base) return ERROR;
   *e=*(S.top-1);
   return OK;
} 

//符号匹配的检验
Status Matching(SqStack &S){
    
    
	SElemType e,out;
	printf("请依次输入符号(输入#结束):\n");
 while(e != '#')    // 如果匹配不通过,必须手动输入 # 来结束程序,可输出当前剩余元素,若匹配,则不输出 
    {
    
    
        scanf("%c", &e);
        getchar();
        if(e == '#') break;       // 不写这行会出现输入一个 # 不会退出
        if(StackLength(S) >= 0)     
        {
    
    
            int i = 0;
            GetTop(S, &out);       // 获取当前栈顶元素
            Push(S, &e);            // 入栈
            if(abs(out-e) == 1 || abs(out-e) == 2)  // 如果括号匹配通过
            {
    
    
                while(i != 2)         // 要出栈两次,因为新的元素已经插入,此时栈顶两个元素是匹配成功的,删除的是这两个元素
                {
    
    
                    Pop(S, &out);         
                    i ++;
                }
                if(StackEmpty(S))    // 顺序栈为空,跳出循环,提示通过
                {
    
    
                    printf("\n符号匹配!\n");
                    break;
                }
                continue;
            }
        }
    }
    {
    
       // 出栈,查看当前剩余元素
        printf("\n栈的元素为:\n");
        while(S.top != S.base)
        {
    
    
            Pop(S, &e);
            printf("%c ", e);
        }
    }
    return OK;
}

int main(){
    
    
	SqStack S;
    
//    判断是否初始化成功是不能删除的,删除后程序运行异常 
	if(InitStack(S)) printf("\n初始化成功!\n");
    else exit(0);
    
    Matching(S);
    return OK;
} 

  • The symbols match, and the running results are as follows:
    insert image description here
  • If the symbols do not match, you need to manually enter #, and the elements of the unmatched stack will be displayed

insert image description here

References: Bracket Test

Guess you like

Origin blog.csdn.net/m0_52900946/article/details/123867465