代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct SqStack
{
char data;
struct SqStack* next;
};
SqStack* InitStack(SqStack* &S)
{
S=(SqStack*)malloc(sizeof(SqStack));
S->data=0;
S->next=NULL;
return S;
}
int Push(SqStack* S,char e)
{
SqStack p;
p=(SqStack)malloc(sizeof(SqStack));
p->data=e;
p->next=S->next;
S->next=p;
return 1;
}
int Pop(SqStack* S,char &e)
{
SqStack *p;
if(S->next==NULL)
return 0;
else
{
p=S->next;
e=p->data;
S->next=p->next;
free(p);
return 1;
}
}
int GetTop(SqStack* S,char &e)
{
if(S->next==NULL)
return 0;
e=S->next->data;
return 1;
}
int Match(char a,char b)
{
if(a+1==b||a+2==b)//左右括号ASCII码相差1或2
return 1;
return 0;
}
int Check(SqStack* S,char *str)
{
int i=0;
char e;
while(str[i])
{
switch(str[i])
{
case'(':
case'[':
case'{':
Push(S,str[i]);
break;
case')':
case']':
case'}':
if(S->next==NULL)
{
printf("no!");
}
else
{
GetTop(S,e);
if(Match(e,str[i]))
Pop(S,e);
}
break;
default:
break;
}
i++;
}
if(S->next==NULL)
printf("yes!");
else
printf("no!");
}
int main()
{
SqStack* S;
char str[100];
printf("input:");
scanf("%s",str);
Check(S,str);
}
没有输出结果