({[入栈 )}]于栈顶比较
<span style="font-size:14px;">#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
char c;
struct node* pnext;
}qnode,*pnode;
typedef struct stack
{
pnode top;
pnode botton;
}qstack,*pstack;
bool empty(pstack s)
{
if(s->top==s->botton)
return true;
else
return false;
}
void init(pstack s)
{
s->top=(pnode)malloc(sizeof(node));
s->botton=s->top;
s->botton->pnext=NULL;
}
void push(pstack s,char c_)
{
pnode q;
q=(pnode)malloc(sizeof(qnode));
q->c=c_;
q->pnext=s->top;
s->top=q;
}
void pop(pstack s)
{
char e;
pnode temp;
if(empty(s))
cout<<"erro";
else
{
e=s->top->c;
temp=s->top;
s->top=temp->pnext;
free(temp);
}
}
char gettop(pstack s)
{
char e;
e=s->top->c;
return e;
}
int length(pstack s)
{
int i=0;
pnode q;
q=s->top;
while(q!=s->botton)
{
i++;
q=q->pnext;
}
return i;
}
int judge(pstack s,char a[],int i)
{
int j=0;
char k;
for(j=0;j<i;j++)
{
k=a[j];
if(a[j]=='(' || a[j]=='{' || a[j]=='[')
{
push(s,a[j]);
}
else if(a[j]==')')
{
k=gettop(s);
if(k=='(')
pop(s);
else
return -1;
}
else if(a[j]==']')
{
k=gettop(s);
if(k=='[')
pop(s);
else
return -1;
}
else if(a[j]=='}')
{
k=gettop(s);
if(k=='{')
pop(s);
else
return -1;
}
}
return length(s);
}
int main()
{
int p;
qstack s;
char a[100];
char ch;
int i=0;
while( ( ch = getchar() ) != '\n' )
{
a[i]=ch;
i++;
}
init(&s);
p=judge(&s,a,i);
if(p==0)
cout<<"yes";
if(p>0)
cout<<") ] }";
if(p<0)
cout<<"( [ {";
}</span>