一、括号匹配
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.\r\n";
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."<<endl;
return '\0';
}
// Step 2. Update the top.
paraStackPtr->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<paraLength;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 <iostream>
#include <stdio.h>
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;i<paraStack->top;i++)
{
cout<<paraStack->data[i]<<" ";
}
cout<<endl;
}
void push(CharStackPtr paraStackPtr, int paraValue)
{
// Step 1. Space check.
if(paraStackPtr->top >= MaxSize-1)//top从0开始计数,所以可以看成是下标
{
cout<<"Cannot push element: stack full.\r\n";
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."<<endl;
return '\0';
}
// Step 2. Update the top.
paraStackPtr->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<paraLength;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 \r\n", tempExpression, tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression, 8);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression, 6);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression, 2);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
tempExpression = "((())";
tempMatch = bracketMatching(tempExpression, 5);
printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}
int main() {
// pushPopTest();
bracketMatchingTest();
system("pause");
return 0;
}
7.运行结果
三、总结
栈是一种经典的数据结构,利用它“先进后出,后进先出”的特性是解题的关键。