lem,GList &x) {elem->utype=x.utype; if(x.utype==INTGR) elem->intinfo=elem->intinfo; if(x.utype==CH&&x.charinfo!=')') elem->charinfo=x.charinfo; return *elem; } //返回由ls指示的广义表的第一个元素的值 GList &GList::Head(GList *&ls) {static GList *temp; if(ls==NULL) {cout<<"Illegal head operation.\n";exit(1);} else { temp=new GList; ls=ls->first; temp=ls; if(ls->utype==INTGR) temp->intinfo=ls->intinfo; if(ls->utype==CH) temp->charinfo=ls->charinfo;} temp->first=ls; return *temp; } //返回广义表除第一个元素以外其它元素组成的表 GList *GList::Tail() {if(first==NULL) {cout<<"Illegal head operation.\n";exit(1);} return first->first; } //返回广义表的第一个元素 GList *GList::First() {if(hlink==NULL) return NULL; else return hlink; } //返回由elem指示的表元素的直接后继元素 GList *GList::Next(GList *elem) {if(elem->first==NULL) return NULL; else return elem->first; } //插入元素x作为由ls指示的广义表的第一元素 GList *GList::InsertGL(GList *&ls,GList &x) {if(ls->hlink==NULL) ls->hlink=&x; else { static GList *temp=new GList; temp->utype=x.utype; temp->first=ls->first; temp->intinfo=x.intinfo; ls->hlink=temp; ls->first=temp;} return ls; } //返回一个以x为头,由ls指示的广义表为尾的新表 GList *GList::Addon(GList *ls,GList &x) {static GList *p=new GList; p->utype=x.utype; if(x.utype==INTGR) p->intinfo=x.intinfo; if(x.utype==CH) p->charinfo=x.charinfo; p->first=Copy(ls); ls=p; return ls; } //将广义表的头元素重置为x void GList::setHead(GList *&ls,GList &x) {static GList *temp=new GList; temp->utype=x.utype; if(x.utype==INTGR) temp->intinfo=x.intinfo; if(x.utype==CH&&x.charinfo!=')') temp->charinfo=x.charinfo; temp->first=ls->first->first; ls->first=temp; ls->hlink=temp; } //将elem2插到表中元素elem1后 void GList::setNext(GList *elem1,GList *elem2) {GList *temp; while(elem1->first!=NULL) {temp=elem1->first; elem2->first=temp->first; delete temp;} elem1->first=elem2; } //将x定义为由ls指示的广义表的尾 void GList::setTail(GList *&ls,GList &x) {static GList *r,*q,*p,*temp=new GList; temp->utype=x.utype; if(x.utype==INTGR) temp->intinfo=x.intinfo; if(x.utype==CH&&x.charinfo!=')') temp->charinfo=x.charinfo; r=ls; while(!(r->charinfo!=')'&&r->first->first->first==NULL)) {p=r->first;r=r->first;} q=p->first; p->first=temp; temp->first=q; } //由ls指示的广义表的复制 GList *GList::Copy(GList *ls) {static GList *gh,*q,*p=new GList; q=p; if(ls) do { gh=new GList; p->utype=ls->utype; switch(ls->utype) {case INTGR:p->intinfo=ls->intinfo;break; case CH:p->charinfo=ls->charinfo;break; case LST:p->hlink=ls->hlink;break; } p->first=ls->first; ls=ls->first; p=gh; }while(ls->first!=NULL); p->first=NULL; return q; } //求由ls指示的非递归表的深度 int GList::depth(GList *&ls) {GList *temp=ls; int m=0; if(temp->first==NULL) return 0; do {if(temp->utype==LST) m++; temp=temp->first; }while(temp!=NULL); return m; } //判断广义表是否相等的重载函数 int GList::operator ==(GList &m) {int k=equal(first,m.first); return k; } //判断广义表是否相等 int GList::equal(GList *s,GList *t) {int x; if(s->first==NULL&&t->first==NULL) return 1; if(s->first==NULL&&t->first==NULL&& s->first->utype==t->first->utype) {if(s->first->utype==INTGR) if(s->first->intinfo==t->first->intinfo) x=1; else x=0; else if(s->first->utype==CH) if(s->first->charinfo==t->fir |