st->charinfo) x=1; else x=0; else x=equal(s->first->first,t->first->first); if(x) return equal(s->first,t->first); } return 0; } //删除广义表中含数x或结点x的操作 GList *GList::delvalue(GList *&ls,int x) {static GList *r,*p,*q=ls; p=ls; while(ls!=NULL) {p=ls->first; while(p!=NULL&&p->utype==INTGR&&p->intinfo==x) {r=p->first; delete p; p=r;break;} if(p==r){ls->first=r;break;} ls=ls->first; } return q; } //删除广义表中含数x或结点x的操作 GList *GList::delvalue(GList *&ls,char x) {static GList *r,*p,*q=ls; p=ls; while(ls!=NULL) {p=ls->first; while(p!=NULL&&p->utype==CH&&p->charinfo==x) {r=p->first; delete p; p=r;break;} if(p==r){ls->first=r;break;} ls=ls->first; } return q; } //S是广义表的书写形式串,由S创建广义表GL GList *GList::CreateGList(char *&s) {GList *r,*q,*p; p=q=new GList; p->first=NULL; char *sub=new char[30]; char *hsub=new char[30]; for(int i=0;i<30;i++) hsub[i]=sub[i]='\0'; strncpy(sub,s,strlen(s)); sub[strlen(s)]='\0'; cout<<"欲创建的广义表="< cout<<"广义表的长度="< if(strlen(sub)==0||!strcmp(sub,"()")) p->first=NULL; else {do {sever(sub,hsub); if(strlen(hsub)==1) {if(48<=hsub[0]&&hsub[0]<=57) {r=new GList;r->first=NULL; p->utype=INTGR;p->intinfo=atoi(hsub); p->first=r;p=r;} else {if((65<=hsub[0]&&hsub[0]<91)||(hsub[0]>=97&&hsub[0]<123)||hsub[0]==')') {r=new GList;r->first=NULL; p->utype=CH;p->charinfo=hsub[0]; p->first=r;p=r;} else {if(hsub[0]=='(') {r=new GList;r->first=NULL; p->utype=LST;p->hlink=r; p->first=r;p=r; }}}} }while(*sub!='\0'); p->first=NULL; } return q; } //建立广义表时调用的过程 int GList::sever(char *&str1,char *&hstr1) {char ch;int i=0,k=0,j=0; int n=strlen(str1); static int m=1; if(m==1) while(j {ch=str1[j];j++;m++; if(ch=='\0') break; if(ch=='(') k++; else if(ch==')') k--;} if(k!=0) return 0; for(i=0;i {ch=str1[i]; if(ch=='\0') break; if(ch==',') {for(int i0=i;i0 str1[i0]=str1[i0+1]; break;}} if(n>1) { strncpy(hstr1,str1,1); strncpy(str1,str1+1,n-1);str1[n-1]='\0'; return 1;} else if(n==1) {strncpy(hstr1,str1,1); str1[0]='\0';return 1;} else return 0; } //广义表的输出 void GList::prtGlist(GList *h) {GList *q,*p=h; if(h) do {if(p->utype==INTGR) {q=p->first; if(q->charinfo==')'||q->first==NULL) cout<intinfo; else cout<intinfo<<','; p=p->first;} else {if(p->utype==CH) {q=p->first; if(q->charinfo==')'||q->first==NULL) cout<charinfo; else cout<charinfo<<','; p=p->first;} else if(p->utype==LST) {cout<<'(';p=p->first;} } }while(p->first!=NULL); }
类的调用如下 [cpp] #include "stdafx.h" #include "guangyi.h" static GList *GL,*GL1,*GL2,*GL3; static GList GU,GU1,GU2,GU3,GU4; void main() {cout<<"运行结果:\n"; char *a="(A,(B),(3,5,7),(a,b),((C)),D)"; char *b="(A,(B),(3,5),(a,b),7)"; char *c="(9,(B),(3,5),(a,b))"; GU3.utype=INTGR;GU3.intinfo=9; GU3.first=GL2; GL=GU.CreateGList(a); cout<<"返回的指针值GL="< cout<<"创建后的广义表="; GU.prtGlist(GL); cout<<"\n广义表GL的深度="< GL=GU.delvalue(GL,5); cout<<"\n删除5后的广义表GL=";GU.prtGlist(GL); GL=GU.delvalue(GL,'a'); cout<<"\n删除a后的广义表GL=";GU.prtGlist(GL); cout<<"\n将GU3定义为由ls指示的广义表的尾:"; GU.setTail(GL,GU3);GU.prtGlist(GL); cout<<"\n重置广义表的头元素后的广义表:"; GU.setHead(GL,GU3);GU.prtGlist(GL); GL1=GU.Copy(GL); cout<<"\n复制后的广义表GL1="; GU.prtGlist(GL1); cout<<"\n广义表GU的第一个元素的值:\n"; GU2=GU.Head(GL1); cout<<"GU2.utype="< if(GU2.utype==INTGR) cout<<",GU2.intinfo="< if(GU2.utype==CH) cout<<",GU2.charinfo="< GU=GU.Info(GL1); cout<<"广义表GU的第一个元素的值:\n"; cout<<"GU.utype="< if(GL1->utype==INTGR) cout<<",GU.intinfo="< if(GL1->u
|