栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

括号匹配利用栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

括号匹配利用栈

目录
  • 一、括号匹配
    • 1.栈结构体
    • 2.栈的初始化
    • 3.压栈
    • 4.出栈
    • 5.核心算法--借助栈
    • 6.完整代码
    • 7.运行结果
  • 三、总结

一、括号匹配 1.栈结构体
typedef struct CharStack
{
	int top;
	int data[MaxSize];
}*CharStackPtr;

用int类型,是把括号利用了强制转换

2.栈的初始化
CharStackPtr charStackInit() 
{
	CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
	resultPtr->top = -1;

	return resultPtr;
}

3.压栈
void push(CharStackPtr paraStackPtr, int paraValue)
{
	// Step 1. Space check.
	if(paraStackPtr->top >= MaxSize-1)//top从0开始计数,所以可以看成是下标
	{
		cout<<"Cannot push element: stack full.rn";
		return;
	}

	// Step 2. Update the top.
	paraStackPtr->top ++;

	// Step 3. Push element.
    paraStackPtr->data[paraStackPtr->top] = paraValue;
}

4.出栈
int pop(CharStackPtr paraStackPtr)
{
	// Step 1. Space check.
	if(paraStackPtr->top < 0)
	{
		cout<<"Cannot pop element: stack empty."<top--;

	// Step 3. Pop element.

	return paraStackPtr->data[paraStackPtr->top + 1];
}
5.核心算法–借助栈
bool bracketMatching(char* paraString, int paraLength)
{
	// Step 1. Initialize the stack through pushing a '#' at the bottom.
    CharStackPtr tempStack = charStackInit();

	push(tempStack, '#');//由于初始化 top=-1,用‘#’占用top=-1 作为空位

	char tempChar, tempPopedChar;

	// Step 2. Process the string.
	for(int i = 0;i
		tempChar = paraString[i];


		switch(tempChar)
		{
			case '(':
				push(tempStack, tempChar);
				break;

			case '[':
				push(tempStack, tempChar);
				break;

			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:
				// Do nothing.
				break;
		}
	}

	tempPopedChar = pop(tempStack);
	if (tempPopedChar != '#')//对出现部分匹配成功,左括号还有没有匹配成功的情况,进行处理。eg:(((())
		return false;

	return true;

}
6.完整代码
#include 
#include 

using namespace std;

#define MaxSize 10

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

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

	return resultPtr;
}

void outputStack(CharStackPtr paraStack)
{
	for(int i = 0;itop;i++)
	{
		cout<data[i]<<" "; 
	}

	cout<
	// Step 1. Space check.
	if(paraStackPtr->top >= MaxSize-1)//top从0开始计数,所以可以看成是下标
	{
		cout<<"Cannot push element: stack full.rn";
		return;
	}

	// Step 2. Update the top.
	paraStackPtr->top ++;

	// Step 3. Push element.
    paraStackPtr->data[paraStackPtr->top] = paraValue;
}

int pop(CharStackPtr paraStackPtr)
{
	// Step 1. Space check.
	if(paraStackPtr->top < 0)
	{
		cout<<"Cannot pop element: stack empty."<top--;

	// Step 3. Pop element.

	return paraStackPtr->data[paraStackPtr->top + 1];
}

bool bracketMatching(char* paraString, int paraLength)
{
	// Step 1. Initialize the stack through pushing a '#' at the bottom.
    CharStackPtr tempStack = charStackInit();

	push(tempStack, '#');//由于初始化 top=-1,用‘#’占用top=-1 作为空位

	char tempChar, tempPopedChar;

	// Step 2. Process the string.
	for(int i = 0;i
		tempChar = paraString[i];


		switch(tempChar)
		{
			case '(':
				push(tempStack, tempChar);
				break;

			case '[':
				push(tempStack, tempChar);
				break;

			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:
				// Do nothing.
				break;
		}
	}

	tempPopedChar = pop(tempStack);
	if (tempPopedChar != '#')//对出现部分匹配成功,左括号还有没有匹配成功的情况,进行处理。eg:(((())
		return false;

	return true;

}

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


	tempExpression = "( )  )";
	tempMatch = bracketMatching(tempExpression, 6);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);

	tempExpression = "()()(())";
	tempMatch = bracketMatching(tempExpression, 8);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);

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


	tempExpression = ")(";
	tempMatch = bracketMatching(tempExpression, 2);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);

	tempExpression = "((())";
	tempMatch = bracketMatching(tempExpression, 5);
	printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch);
}

int main() {
	// pushPopTest();
	bracketMatchingTest();

	system("pause");
	return 0;
}
7.运行结果

三、总结

栈是一种经典的数据结构,利用它“先进后出,后进先出”的特性是解题的关键。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868989.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号