#define STACK_MAX_NUM 10
typedef struct tag_Stack_S
{
char StackData[STACK_MAX_NUM];
int top;
}Stack_S;
void Stack_Init(Stack_S *sl)
{
sl->top = -1;
}
//进栈
int Stack_In(Stack_S *sl, char Element)
{
if (sl->top == STACK_MAX_NUM - 1)
{
return T_ERR;
}
sl->top++;
sl->StackData[sl->top] = Element;
return T_OK;
}
//出栈
int Stack_Out(Stack_S *sl , char *Element)
{
if((sl->top == -1) || (NULL == Element))
{
return T_ERR;
}
*Element = sl->StackData[sl->top--];
return T_OK;
}
int Stack_Match(char *exps)
{
Stack_S stPoly;
int i = 0;
char Data = 0;
int nomatch = 0;
if(NULL == exps)
{
return T_ERR;
}
Stack_Init(&stPoly);
while((exps[i] != '/0') && (nomatch == 0))
{
switch(exps[i])
{
case '(':
case '{':
case '[':
Stack_In(&stPoly,exps[i]);
break;
case ')':
Stack_Out(&stPoly, &Data);
if(Data != '(')
{
nomatch = 1;
}
break;
case '}':
Stack_Out(&stPoly, &Data);
if(Data != '{')
{
nomatch = 1;
}
break;
case ']':
Stack_Out(&stPoly, &Data);
if(Data != '[')
{
nomatch = 1;
}
break;
default :
break;
}
i++;
}
if(nomatch == 0)
{
return T_OK;
}
else
{
return T_ERR;
}
}
栈的顺序实现以及括号匹配判断程序
最新推荐文章于 2022-10-22 17:05:17 发布