欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

栈的应用(括号匹配)

程序员文章站 2022-07-14 23:18:06
...
/******************************************************
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);
}


栈的应用(括号匹配)