# Application of stack -- bracket matching problem

## 1, Problem introduction:

Suppose an arithmetic expression contains parentheses, square brackets and curly brackets, write a function to determine whether the parentheses in the expression are correctly matched.

## 2, Algorithm idea:

There are four cases of bracket matching:

• The pairing order of left and right brackets is incorrect
• Left bracket is more than right bracket
• Right bracket is more than left bracket
• Left and right brackets match successfully
Specific implementation method: sequence scan arithmetic expression (expressed as a string), when three types of left parentheses are encountered, let the parentheses stack. When a certain type of right bracket is scanned, compare whether the top bracket of the current stack matches it. If it matches, Backstack will continue to judge: if the top symbol of the current stack is different from the currently scanned bracket, the matching order of the left and right brackets is incorrect. If the string is currently a certain type of right bracket and the stack is empty, then the right bracket is more than the left bracket; if the stack is not empty at the end of the string cycle scanning (that is, there is a certain type of left bracket in the stack), then the left bracket is more than the right bracket; if the above three situations do not occur, then the left bracket matches correctly.

## 3, Code implementation (Visual Studio 2017 development environment)

```#pragma once
#include<windows.h>
typedef struct Stacknode {
Elemtype data;
struct Stacknode *next;
}Stacktype;
//initialization
void InitStack(Stacktype **top) {
*top = (Stacktype *)malloc(sizeof(Stacktype));
(*top)->next = NULL;
}
//Push operation
int pushStack(Stacktype *top, Elemtype x) {
Stacktype *p;
p = (Stacktype*)malloc(sizeof(Stacktype));//Application node
p->data = x;
p->next = top->next;
top->next = p;
return true;
}
//Stack out operation
Elemtype popStack(Stacktype *top) {
Stacktype *p;
Elemtype x;
if (top->next == NULL) {
printf("Empty stack!!");
return NULL;
}
else {
p = top->next;
top->next = p->next;
x = p->data;
free(p);
return x;
}
}
//Data element at the top of stack
Elemtype GetTop(Stacktype *top, Elemtype *x) {
if (top->next == NULL) {
return NULL;
}
else {
*x = top->next->data;
return (top->next->data);
}
}
//Judge stack is not empty
int StackNotEmpty(Stacktype *top) {
if (top->next != NULL) {
return 1;
}
else {
return 0;
}
}
//parenthesis matching
void bracket(char exp[], int n) {
//Determine whether the left and right parentheses of the string exp with n characters are matched correctly
Stacktype *myStack;
int i;
char c;
InitStack(&myStack);//Initial Stack
for (i = 0; i < n; i++) {
if ((exp[i] == '(') || (exp[i] == '[') || (exp[i] == '{')) {//Left parenthesis encountered, stack
pushStack(myStack, exp[i]);
}
else if ((exp[i] == ')') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c == '(') {//Judgment '()'
popStack(myStack);
}
else if ((exp[i] == ')') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c != '(') {
printf("The pairing order of left and right brackets is incorrect!\n");
return;
}
else if ((exp[i] == ']') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c == '[') {//Judgment '[]'
popStack(myStack);
}
else if ((exp[i] == ']') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c != '[') {
printf("The pairing order of left and right brackets is incorrect!\n");
return;
}
else if ((exp[i] == '}') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c == '{') {//Judge '{}'
popStack(myStack);
}
else if ((exp[i] == '}') && StackNotEmpty(myStack) && GetTop(myStack, &c) && c != '{') {
printf("The pairing order of left and right brackets is incorrect!\n");
return;
}
else if ((exp[i] == ')') || (exp[i] == ']') || (exp[i] == '}') && !StackNotEmpty(myStack)) {
printf("Right bracket is more than left bracket!\n");
return;
}
}
if (StackNotEmpty(myStack)) {//When the character array traverses, the stack is not empty, then the left bracket is more than the right bracket
printf("Left bracket is more than right bracket!\n");
return;
}
else {
printf("**Left and right brackets match successfully**\n");
}

}
```

Source file:

```#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char Elemtype;
#include"stack.h"
int main() {
int n;//Character array size
printf("Please enter the character array size\n");
scanf("%d",&n);
char *a;
a = (char *)calloc(n, sizeof(char));//Request n spaces of char type for dynamic array
getchar();
printf("Please enter the contents of character array\n");
for (int i = 0; i < n; i++) {
scanf("%c", &a[i]);
}
printf("\n");
bracket(a,n);
system("pause");
return 0;
}
```

## 4, Test results    Tags: Windows

Posted on Sun, 07 Jun 2020 05:36:24 -0400 by cainy1982