1.代码
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define STACK_MAX_SIZE 10
/**
* Linear stack of inters.the key is data.
*/
typedef struct CharStack{
int top;
int data[STACK_MAX_SIZE];
}*CharStackPtr;
/**
*output the stack.
*/
void outputStack(CharStackPtr paraStack){
int i;
for(i=0;i<=paraStack->top;i++){
printf("%c",paraStack->data[i]);
}//of for i
printf("\r\n");
}//of outputStack
/**
*Initialize an empty char stack.No error checking for this function.
*@param paraStackPtr the pointer to the stack.it must be a pointer to change the stack.
*@param paraVlaues an int array storting all elements.
*/
CharStackPtr charStackInit(){
CharStackPtr resultPtr=(CharStackPtr)malloc(sizeof(struct CharStack));
resultPtr->top=-1;
return resultPtr;
}//of CahrStackInit
/**
*Psh an element to the stack.
*@param paravalue the value to be pushed.
*/
void push(CharStackPtr paraStackPtr,int paraValue){
//Step1.Space check.
if(paraStackPtr->top>=STACK_MAX_SIZE-1){
printf("Cannot push element:stack full");
return;
}//of if
//Step2.Update the top.
paraStackPtr->top++;
//Step3.Push element.
paraStackPtr->data[paraStackPtr->top]=paraValue;
}//of push
/**
*Pop an element from the stack.
*@return the poped value.
*/
char pop(CharStackPtr paraStackPtr){
//step1.space check.
if(paraStackPtr->top<0){
printf("cannot pop element :stack empty");
return '\0';
}//of if
//Step2.Update the top.
paraStackPtr->top--;
//step3.pop element
return paraStackPtr->data[paraStackPtr->top+1];
}//of pop
/**
*test the push function
*/
void pushPopTest(){
printf("----pushpopTest begins.----");
//initialize.
CharStackPtr tempStack=charStackInit() ;
printf("After initialization,the stack is:");
outputStack(tempStack);
//pop.
char ch;
for(ch='a';ch<'m';ch++){
printf("Pushing %c.\r\n",ch);
push(tempStack,ch);
outputStack(tempStack);
}//of for i
//pop
int i;
for(i=0;i<3;i++){
ch = pop(tempStack);
printf("Pop %c.\r\n",ch);
output(tempStack);
}//of for i
printf("----pushPopTest ends.----");
}//of pushPopTest
/**
*Is the bracket matching?
*
*@param paraString The given expression.
*@return Match or not.
*/
bool bracketMatching(char *paraString,int paraLength){
//Step1.Initialize the stack through pushing a '# at the bottom.
CharStackPtr tempStack = charStackInit();
push(tempStack,'#');
char tempChar,tempPopedChar;
//Step2.Process the string.
int i;
for(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;
}//of if
break;
case ']':
tempPopedChar = pop(tempStack) ;
if(tempPopedChar != '['){
return false;
}//of if
break;
case '}':
tempPopedChar = pop(tempStack);
if(tempPopedChar !='{'){
return false;
}//of if
break;
default :
//Doing nothing.
break;
}//of switch
}//of for i
tempPopedChar = pop(tempStack);
if(tempPopedChar != '#'){
return false;
}//of if
return true;
}//of brakeetMatching.
/**
*Unit
*/
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);
}//of bracketMatchingTest
/**
*The entrancee
*/
void main(){
//pushPopTest();
bracketMatchingTest();
}//of main
2.运行结果
3.小结
栈是一种重要的数据结构,它遵循后进先出(LIFO)的原则。在括号匹配的问题中,栈的应用尤为广泛和有效。括号匹配是检查一个字符串中的括号是否正确配对的问题,其中可以包括小括号“()”、中括号“[]”和大括号“{}”。
栈在括号匹配问题中的主要应用步骤:
1)初始化栈:
在处理括号匹配问题之前,我们需要一个空栈来存储待匹配的左括号。
2)遍历字符串:
从左到右遍历输入的字符串,检查每个字符。
3)处理左括号:
当遇到左括号(小括号“(”、中括号“[”或大括号“{”)时,我们将其压入栈中。
4)处理右括号:
当遇到右括号时,我们需要检查栈顶元素。如果栈为空,说明没有匹配的左括号,因此括号不匹配。如果栈不为空,我们弹出栈顶元素,并检查它是否与当前的右括号匹配。如果匹配,则继续遍历;如果不匹配,则括号不匹配。
5)结束检查:
遍历完整个字符串后,如果栈为空,说明所有括号都正确匹配;如果栈不为空,说明还有未匹配的左括号,因此括号不匹配。
需要注意,虽然栈在括号匹配问题中非常有效,但也可能存在其他方法来解决同样的问题。此外,在实际应用中,还需要考虑错误处理和边界情况,例如空字符串或包含非括号字符的字符串等。