#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 5000
typedef struct stack
{
char base[100];
int top;
} SqStack;
SqStack *InitStack()
{
SqStack *st;
if(st=(SqStack *)malloc(sizeof(SqStack)))
{
st->top=-1;
return st;
}
return NULL;
}
int Pop(SqStack *st)
{
if(st->top==-1)
{
printf("stack is empty\n");
return 0;
}
return st->base[st->top--];
}
void Push(SqStack *st,char c)
{
if(st->top+1== MAXLEN)
{
printf("stack is full\n");
return ;
}
st->base[++st->top]=c;
}
int Clearstack(SqStack *st)
{
return st->top=-1;
}
void check_symbol(SqStack *st,char *a)
{
int i;
Push(st,a[0]);
for(i=1; i<strlen(a); i++)
{
if ((a[i]==']'&&st->base[st->top]=='[')||(a[i]==')'&&st->base[st->top]=='(')||(a[i]=='}'&&st->base[st->top]=='{')||(a[i]=='>'&&st->base[st->top]=='<'))
Pop(st);
else
Push(st,a[i]);
}
if(st->top==-1)
{
printf("YES\n\n");
}
else
{
printf("NO\n\n");
}
}
void main()
{
while (1)
{
char s[50];
SqStack *st;
st=InitStack();
printf("Please input a string:\n");
scanf("%s",s);
check_symbol(st,s);
}
}
栈的应用---括号匹配
最新推荐文章于 2023-11-17 14:09:52 发布