Bracket matching using stack

Construct stack function and use stack to achieve bracket matching

The following code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Create a stack, use the stack to find binary to decimal, convert to octal, destroy, empty, Push, Pop
//Use the stack to find the reverse Polish algorithm (using characters to convert to double-precision variables), and use the stack to check whether the parentheses are correct
//Create a linked list stack with insert and delete operations
#define INITNUM 20
#define INCREASENUM 5
typedef  char elem;
typedef struct Stack {
	element * top;
	element * base;
	int StackSize;
}sqStack;
void InitStack(sqStack*s) {
	s->base = (elem*)malloc(INITNUM*sizeof(elem));
	s->top = s->base;
	s->StackSize = INITNUM;
}
void Push(sqStack*s,elem e) {
	//When adding data, when the size of the data exceeds the predetermined value, the storage space of the entire linked list needs to be changed
	if (s->top - s->base >= s->StackSize) {
		s->base = (elem*)realloc(s->base, (s->StackSize + INCREASENUM) * sizeof(elem));
		s->top = s->base + s->StackSize;
		s->StackSize = s->StackSize + INCREASENUM;
		//StackSize changed
	}
	*(s->top) = e;
	s->top++;//Move the top of the s stack one place
}
//Pop the stack and get the popped data
void Pop(sqStack*s, elem*e) {
	if (s->top == s->base)
		return;
	else {
		*e = *(--s->top);//Change the original e value
		// The value below s->top is the data to be popped from the stack
	}
}
void ClearStack(sqStack*s) {
	s->top = s->base;//Empty the list
}
int StackLen(sqStack*s) {
	return s->top - s->base;
	//Get the current length of the linked list
}
void DestroyStack(sqStack*s) {
	int len = INITNUM;
	for (int i = 0; i < len; i++) {
		free(s->base);
		s->base++;
		//In fact, it is to empty the stack
	}
	s->base = s->top = 0;
	s->StackSize = 0;
}


//function to check if parentheses match
int IsAlign (sqStack*s) {
	char c,temp=0;
	printf("Please enter a series of brackets, ending with the Enter key: ");
	scanf("%c", &c);
	//scanf() When inputting characters, the buffer space of the keyboard is 1, that is, read and save
	//When entering an int variable, its space size is 4 bytes, which can be stored after carriage return
	while (c != '\n') {
		switch (c)
		{
		case '{':
			Push(s, c);
			break;
		case '[':
			Push(s, c);
			break;
		case '(':
			Push(s, c);
			break;
		case ')':
			Pop(s, &temp);//Pop the number in the stack at that time
			if (temp == '(')
				break;
			else
				return 0;
		case ']':
			Pop(s, &temp);
			if (temp == '[')
				break;
			else
				return 0;
		case '}':
			Pop(s, &temp);
			if (temp == '{')
				break;
			else
				return 0;
		default:
			break;
		}
		scanf("%c", &c);
	}
	if ((s->top != s->base) || !temp)//When no character is entered or only half brackets are entered, the return value is 0
		return 0;
	return 1;
}
int main() {
	sqStack s;
	InitStack(&s);//Be sure to initialize first, otherwise the base has no space for assignment
	if (IsAlign(&s))
		printf("Successful match!\n");
	else
		printf("Match failed!\n");
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519612&siteId=291194637