栈的应用(括号匹配)

来源:互联网 发布:java http代理服务器 编辑:程序博客网 时间:2024/05/21 21:41
/******************************************************Description  :Check ()[]{}if matchInput        :char *pStoreOutPut       :NoneReturn 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);}