#include<stdio.h>
#include<string.h>
int len;
struct stack
{
char str[55];
int top;
};
typedef struct stack Stack;
Stack s;
void init()
{
s.top=-1;
}
void push(char element)
{
s.top++;
s.str[s.top]=element;
}
void pop()
{
s.top--;
}
int i;
char a[55];
int main()
{
scanf("%s",&a);
len=strlen(a);
init();
push(a[0]);
for(i=1;i<len;i++)
{ if(s.str[s.top]=='('&&a[i]==')')
pop();
else
push(a[i]);
}
if(s.top==-1)
printf("这是一个合法序列\n");
}
堆栈的使用(c语言描述)
最新推荐文章于 2025-02-25 10:38:35 发布