您的位置:首页 > 其它

栈的应用(括号匹配)

2017-07-11 08:42 316 查看
/******************************************************
Description  :Check ()[]{}if match
Input        :char *pStore
OutPut       :None
Return Value :
Calls        :
Call By      :
******************************************************/
void MatchCheck(char * pStore)
{
SqStack *pST = (SqStack *)malloc(sizeof(SqStack));
int Ret = 0;
SElemType stSlem = {0};
SElemType *pGetTopElem = (SElemType*)malloc(sizeof(SElemType));
Ret = InitStack(pST);
while(*pStore)
{
memset(&stSlem,0,sizeof(SElemType));
strcpy(stSlem.cStr,pStore);
switch(*pStore)
{
case '(':
case '[':
case '{':
Ret = Push(pST,stSlem);
pStore++;
break;
case ')':
case ']':
case '}':
Ret = Pop(pST,&pGetTopElem);
if((*pGetTopElem->cStr=='(' && *pStore==')')||(*pGetTopElem->cStr=='[' && *pStore==']') || (*pGetTopElem->cStr=='{' && *pStore=='}'))
{
pStore++;
}
else
{
printf("Not Match\n");
return;
}
break;
default:
pStore++;
break;
}
}
if(IsEmpty(pST))
{
printf("Match\n");
}
else
{
printf("Not Match\n");
}
DestroyStack(pST);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: