数据结构 三种括号匹配
- 格式:docx
- 大小:11.68 KB
- 文档页数:3
}
void stack::pair(char *st){
char e;
while(*st){
switch(*st){
case '(':{
push(*st);st+1:{
if(!emptystack()){pop(e);st++;}
else{cout<<"error("<<endl; return;}
#include<iostream>
using namespace std;
typedef struct lnode{
char data;
lnode *next;
}lnode;
class stack{
lnode *top;
public:
stack();
void push(char e);//入栈
void pop(char &e);//出栈
int emptystack();//判空
void pair(char *st);//括号匹配
};
stack::stack(){
top=new lnode;
top->next=NULL;
}
void stack::push(char e){
lnode *s=new lnode;
s->data=e;
s->next=top->next;
top->next=s;
}
void stack::pop(char &e){
lnode *p=top->next;
if(top->next==NULL)return;
e=p->data;
top->next=p->next;
delete p;
}
int stack::emptystack(){
if(top->next==NULL) return 1;
}
void main(){
stack s;
char *st=new char[255];
cin>>st;
s.pair(st);
}
break;
}
case '[':{
push(*st);st++;
break;
}
case ']':{
if(!emptystack()){pop(e);st++;}
else{cout<<"error["<<endl; return;}
break;
}
case '{':{
push(*st);st++;
break;
}
case '}':{
if(!emptystack()){pop(e);st++;}
else{cout<<"error{"<<endl; return;}
break;
}
}
}
if(emptystack()){
cout<<"error)"<<endl;return;
}
else cout<<"pair"<<endl;