栈的应用(括号匹配)

xiaoxiao2021-02-28  127

/****************************************************** 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); }

转载请注明原文地址: https://www.6miu.com/read-50128.html

最新回复(0)