数据结构学习栈的应用(括号匹配)

目录

一.代码展示

1.括号匹配函数

2.测试函数

3.总代码

4.测试结果

二.总结


一.代码展示

这次代码是基于栈的基础代码上来写的。括号匹配是栈的应用之一,就像开心消消乐一样还是比较有趣的。

1.括号匹配函数

                                                               图示

bool bracketMatch(char *paraString, char paraLength){
	//初始化创建栈底
	 stackPtr tempStack= initStack();
	 push(tempStack, '#');
	 char tempChar,tempPopedChar;
	  
	//start
	for(int i=0;i<paraLength;i++){
		tempChar= paraString[i];
		
		switch(tempChar){
			case '(':
			case '[':
			case '{':
				push(tempStack,tempChar);
				break;
			case ')':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '(') {
						return false;
					}
				break;
			case ']':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '[') {
						return false;
					} 
				break;
			case '}':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '}') {
						return false;
					} 
				break;
			default:
				break;
		}
	}
	
	tempPopedChar = pop(tempStack);
	if (tempPopedChar != '#') {
		return false;
	} 
	return true;
}

2.测试函数

void bracketMatchingTest() {
	char* tempExpression = "[2 + (1 - 3)] * 4";
	bool tempMatch = bracketMatch(tempExpression, 17);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


	tempExpression = "( )  )";
	tempMatch = bracketMatch(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

	tempExpression = "()()(())";
	tempMatch = bracketMatch(tempExpression, 8);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

	tempExpression = "({}[])";
	tempMatch = bracketMatch(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


	tempExpression = ")(";
	tempMatch = bracketMatch(tempExpression, 2);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}

3.总代码

#include <stdio.h>
#include <malloc.h>

#define STACK_MAX_SIZE 10

//创建栈结构体 
typedef struct charStack{
	int top;
	
	int data[STACK_MAX_SIZE];
}*stackPtr; 

//输出栈
void outputStack(stackPtr paraStack){
	for(int i=0;i<=paraStack->top;i++){
		printf("%d ",paraStack->data[i]);
	}
	printf("\r\n");
} 

//初始化栈
stackPtr initStack(){
	stackPtr tempStack=(stackPtr)malloc(sizeof(stackPtr));
	tempStack->top=-1;
	
	return tempStack;
} 

//入栈
void push(stackPtr stack,int paraValue){
	//检查空间
	 if (stack->top >= STACK_MAX_SIZE - 1) {
        printf("Cannot push element: stack full.\r\n");
        return;
    }
    
    //update top
	stack->top++;
	
	//添加元素
	 stack->data[stack->top]=paraValue; 
} 

//出栈
int pop(stackPtr stack){
    // 空间检查 
    if (stack->top < 0) {
        printf("Cannot pop element: stack empty.\r\n");
        return '\0';
    }
    
	//update top
	stack->top--;
	
	//返回栈顶 
	return stack->data[stack->top+1];
} 

bool bracketMatch(char *paraString, char paraLength){
	//初始化创建栈底
	 stackPtr tempStack= initStack();
	 push(tempStack, '#');
	 char tempChar,tempPopedChar;
	  
	//start
	for(int i=0;i<paraLength;i++){
		tempChar= paraString[i];
		
		switch(tempChar){
			case '(':
			case '[':
			case '{':
				push(tempStack,tempChar);
				break;
			case ')':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '(') {
						return false;
					}
				break;
			case ']':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '[') {
						return false;
					} 
				break;
			case '}':
				tempPopedChar = pop(tempStack);
					if (tempPopedChar != '}') {
						return false;
					} 
				break;
			default:
				break;
		}
	}
	
	tempPopedChar = pop(tempStack);
	if (tempPopedChar != '#') {
		return false;
	} 
	return true;
}


void pushPopTest() {
    printf("---- pushPopTest begins. ----\r\n");

	// Initialize.
    stackPtr tempStack = initStack();
    printf("After initialization, the stack is: ");
	outputStack(tempStack);

	// Pop.
	for (int i = 1; i < 10; i++) {
		printf("Pushing %d.\r\n", i);
		push(tempStack, i);
		outputStack(tempStack);
	}//Of for i

	// Pop.
	int s;
	for (int j = 0; j< 3; j++) {
		s = pop(tempStack);
		printf("Pop %d.\r\n", s);
		outputStack(tempStack);
	}//Of for i

    printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest

void bracketMatchingTest() {
	char* tempExpression = "[2 + (1 - 3)] * 4";
	bool tempMatch = bracketMatch(tempExpression, 17);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


	tempExpression = "( () )";
	tempMatch = bracketMatch(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

	tempExpression = "(()())";
	tempMatch = bracketMatch(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

	tempExpression = "({}[])";
	tempMatch = bracketMatch(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


	tempExpression = ")(";
	tempMatch = bracketMatch(tempExpression, 2);
	printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}


/**
 The entrance.
 */
int main() {
	//pushPopTest();
	bracketMatchingTest();
}

4.测试结果

Is the expression '[2 + (1 - 3)] * 4' bracket matching? 1
Is the expression '( () )' bracket matching? 1
Is the expression '(()())' bracket matching? 1
Is the expression '({}[])' bracket matching? 1
Is the expression ')(' bracket matching? 0

二.总结

能够判断这是一个有效串的依据是当right=left,right在整个过程中应该大于left,将所有左括弧压入栈中,再一个个的用对应的右括弧消去。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值