数据结构 栈的应用-括号匹配

1.创建栈

typedef struct CharStack{
	int top;
	int data[SIZE];
}*CharStackPtr;

2.输出栈(跟踪数据)

void outputStack(CharStackPtr paraStack){
	for (int i = 0; i <= paraStack -> top; i++){
		printf("%c ",paraStack->data[i]);
	}
	printf("\r\n");
} 

3.初始化

CharStackPtr charStackInit(){
	CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
	resultPtr->top = -1;
	
	return resultPtr;
}

4.输入元素

void push(CharStackPtr paraStackPtr , int paraValue){
	if (paraStackPtr->top >= SIZE - 1){
		printf("Cannot push element : stack full.\n");
	return;
	}
	
	paraStackPtr->top ++;
	
	paraStackPtr->data[paraStackPtr->top] = paraValue;
}

 5.取出元素

char put(CharStackPtr paraStackPtr){
	if (paraStackPtr->top < 0){
		printf("Cannot pop  element : stack  empty.\n");
		return 0;
	}
	
	paraStackPtr->top --;
	
	return paraStackPtr->data[paraStackPtr->top + 1];
}

6.括号匹配

bool bracketMatching(char* paraString, int paraLength) {
	CharStackPtr tempStack = charStackInit();
	push(tempStack,'#');
	char tempChar,tempPopedChar;
	
	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 true;
	}
	
	return true;
}

7.括号测试

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

***  将数组最后一位字符初始化为‘#’,括号匹配结束后数组里将会剩下‘#’,此时可说明括号匹配正确,否则说明只是中途一部分匹配正确而剩余有一半括号未匹配***

总代码

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

#define SIZE 10   

typedef struct CharStack{
	int top;
	int data[SIZE];
}*CharStackPtr;

void outputStack(CharStackPtr paraStack){
	for (int i = 0; i <= paraStack -> top; i++){
		printf("%c ",paraStack->data[i]);
	}
	printf("\r\n");
} 

CharStackPtr charStackInit(){
	CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
	resultPtr->top = -1;
	
	return resultPtr;
}

void push(CharStackPtr paraStackPtr , int paraValue){
	if (paraStackPtr->top >= SIZE - 1){
		printf("Cannot push element : stack full.\n");
	return;
	}
	
	paraStackPtr->top ++;
	
	paraStackPtr->data[paraStackPtr->top] = paraValue;
}

char put(CharStackPtr paraStackPtr){
	if (paraStackPtr->top < 0){
		printf("Cannot pop  element : stack  empty.\n");
		return 0;
	}
	
	paraStackPtr->top --;
	
	return paraStackPtr->data[paraStackPtr->top + 1];
}

void pushPopTest(){
	char ch;
	CharStackPtr tempStack = charStackInit();
	printf("After initialization , the stack is :");
	outputStack(tempStack);
	
	for (char ch = 'a'; ch < 'm'; ch ++){
		printf("Pushing %c.\n",ch);
		push(tempStack,ch);
		outputStack(tempStack);
	}
	
	for (int i = 0; i < 3; i ++){
		ch = pop(tempStack);
		printf("Pop %c.\n",ch);
		outputStack(tempStack);
	}
	
	printf("---pushiPopTest ends.\n");
}

bool bracketMatching(char* paraString, int paraLength) {
	CharStackPtr tempStack = charStackInit();
	push(tempStack,'#');
	char tempChar,tempPopedChar;
	
	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 true;
	}
	
	return true;
}

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

int main(){
	
	printf("这个代码我不理解!!!!\n");
	bracketMatchingTest();
	pushPopTest();
	printf("现在会了吧!!!!\n");
	return 0;
}

运行结构

After initialization , the stack is :
Pushing a.
a
Pushing b.
a b
Pushing c.
a b c
Pushing d.
a b c d
Pushing e.
a b c d e
Pushing f.
a b c d e f
Pushing g.
a b c d e f g
Pushing h.
a b c d e f g h
Pushing i.
a b c d e f g h i
Pushing j.
a b c d e f g h i j
Pushing k.
Cannot push element : stack full.
a b c d e f g h i j
Pushing l.
Cannot push element : stack full.
a b c d e f g h i j
Pop j.
a b c d e f g h i
Pop i.
a b c d e f g h
Pop h.
a b c d e f g
---pushiPopTest ends.
Is the expression '[12 + (1 - 3)] * 4' bracket matching ? 1
Is the expression '( ) )' bracket matching ? 0
Is the expression '()()(())' bracket matching ? 1
Is the expression '({}[])' bracket matching ? 1
Is the expression ')(' bracket matching ?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赛米不会写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值