【数据结构算法】数组、串和广义表

上传人:xins****2008 文档编号:117552595 上传时间:2019-12-05 格式:PPT 页数:68 大小:339.50KB
返回 下载 相关 举报
【数据结构算法】数组、串和广义表_第1页
第1页 / 共68页
【数据结构算法】数组、串和广义表_第2页
第2页 / 共68页
【数据结构算法】数组、串和广义表_第3页
第3页 / 共68页
【数据结构算法】数组、串和广义表_第4页
第4页 / 共68页
【数据结构算法】数组、串和广义表_第5页
第5页 / 共68页
点击查看更多>>
资源描述

《【数据结构算法】数组、串和广义表》由会员分享,可在线阅读,更多相关《【数据结构算法】数组、串和广义表(68页珍藏版)》请在金锄头文库上搜索。

1、数据结构算法 Visual C+ 6.0程序集 侯 识 忠 等编著 中国水利水电出版社 第三章 数组、串和广义表 3、0 变长参数表的应用 / 变长参数表的应用VLArgument.cpp #include #include #include double average( int, .); void main() double w=36.5,x=21.5,y=1.9,z=10.1; coutmnt; p=new struct OLNode;if(!p) exit(-1); h0=p; p-ii=m; p-jj=n; s=mn?m:n; for(i=1;itag.next=p; p-ii=p-

2、jj=0; p-down=p-right=p; hs-tag.next=h0; coutrcv; p=new struct OLNode;if(!p) exit(-1); p-ii=r; p-jj=c; p-tag.e=v; q=hr; while(q-right!=hr p-right=q-right; q-right=p; q=hc; while(q-down!=hc p-down=q-down; q-down=p; return h0; void prmat(struct OLNode *hh) struct OLNode *p,*q; coutright; while(p!=q) co

3、uttag.next; void main() coutmnt; mu=m;nu=n;tu=t; s=mn?m:n; rHead=new OLinks;if(!rHead) exit(-1); cHead=new OLinks;if(!cHead) exit(-1); for(k=0;kije;i!=-1;cinije) p=new OLNode;if(!p) exit(-1); p-ii=i;p-jj=j;p-e=e; /生成结点 p-right=NULL;p-down=NULL; if(rHeadi=NULL) rHeadi=p; else /寻找在行表中的插入位置 for(q=rHead

4、i;(q-right) q-right=p; /完成插入 if(cHeadj=NULL) cHeadj=p; else /寻找在列表中的插入位置 for(q=cHeadj;(q-down) q-down=p;/完成插入 coutmnt; M.mu=m;M.nu=n;M.tu=t; s=mn?m:n; M.rHead=new OLinks;if(!M.rHead) exit(-1); M.cHead=new OLinks;if(!M.cHead) exit(-1); for(k=0;kije;i!=-1;cinije) p=new OLNode;if(!p) exit(-1); p-ii=i;p

5、-jj=j;p-e=e; /生成结点 p-right=NULL;p-down=NULL; if(M.rHeadi=NULL) M.rHeadi=p; else /寻找在行表中的插入位置 for(q=M.rHeadi;(q-right) q-right=p; /完成插入 if(M.cHeadj=NULL) M.cHeadj=p; else /寻找在列表中的插入位置 for(q=M.cHeadj;(q-down) q-down=p;/完成插入 coutmnt; p=new OLNode;if(!p) exit(-1); h0=p; p-ii=m; p-jj=n; s=mn?m:n; for(i=1

6、;itag.next=p; p-ii=p-jj=0; p-down=p-right=p; hs-tag.next=h0; coutrcv; p=new OLNode;if(!p) exit(-1); p-ii=r; p-jj=c; p-tag.e=v; q=hr; while(q-right!=hr p-right=q-right; q-right=p; q=hc; while(q-down!=hc p-down=q-down; q-down=p; return h0; void OLNode:prmat(OLNode *hh) OLNode *p,*q; coutright; while(p

7、!=q) couttag.next; /十字链表相关操作的测试 void main() coutprmat(tt); cin.get();cin.get(); 3、7 广义表的类定义和实现 /广义表的类定义guangyi.h #include #include #include typedef enumINTGR,CH,LSTElemTag; class GList public: ElemTag utype; GList *first; union int intinfo; char charinfo; GList *hlink; ; /构造函数 GList() /返回由elem指示的表元素

8、的值 GList /返回表元素elem的元素值的数据类型 int nodetype(GList *elem) return elem-utype; /将由elem指示的表元素的值修改为x GList /判断广义表是否相等的重载函数 int operator =(GList /判断广义表是否相等 int equal(GList *s,GList *t); /返回由ls指示的广义表的第一个元素的值 GList /返回广义表除第一个元素以外其它元素组成的表 GList *Tail(); /返回广义表的第一个元素 GList *First(); /返回由elem指示的表元素的直接后继元素 GList

9、*Next(GList *elem); /返回一个以x为头,由ls指示的广义表为尾的新表 GList *Addon(GList *ls,GList /由ls指示的广义表的复制 GList *Copy(GList *ls); /求由ls指示的非递归表的深度 int depth(GList * /判断广义表是否为空 bool GlistEmpty() return first=NULL; /将广义表的头元素重置为x void setHead(GList * /将elem2插到表中元素elem1后 void setNext(GList *elem1,GList *elem2); /将x定义为由ls指

10、示的广义表的尾 void setTail(GList * /插入元素x作为由ls指示的广义表的第一元素 GList *InsertGL(GList * /删除广义表中含数x或结点x的操作 GList *delvalue(GList * /删除广义表中含数x或结点x的操作 GList *delvalue(GList * /S是广义表的书写形式串,由S创建广义表GL GList *CreateGList(char * /建立广义表时调用的过程 int sever(char * /广义表的输出 void prtGlist(GList *h); ; /广义表的类实现guangyi.cpp /返回由el

11、em指示的表元素的值 GList pitem-utype=elem-utype; if(elem-utype=LST) pitem-hlink=elem-hlink; if(elem-utype=INTGR) pitem-intinfo=elem-intinfo; if(elem-utype=CH pitem-first=elem-first; return *pitem; /将由elem指示的表元素的值修改为x GList if(x.utype=INTGR) elem-intinfo=elem-intinfo; if(x.utype=CH return *elem; /返回由ls指示的广义表

12、的第一个元素的值 GList if(ls=NULL) coututype=INTGR) temp-intinfo=ls-intinfo; if(ls-utype=CH) temp-charinfo=ls-charinfo; temp-first=ls; return *temp; /返回广义表除第一个元素以外其它元素组成的表 GList *GList:Tail() if(first=NULL) coutfirst=NULL) return NULL; else return elem-first; /插入元素x作为由ls指示的广义表的第一元素 GList *GList:InsertGL(GLi

13、st * 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 p-utype=x.utype; if(x.utype=INTGR) p-intinfo=x.intinfo; if(x.utype=CH) p-charinfo=x.charinfo;

14、p-first=Copy(ls); ls=p; return ls; /将广义表的头元素重置为x void GList:setHead(GList * temp-utype=x.utype; if(x.utype=INTGR) temp-intinfo=x.intinfo; if(x.utype=CH temp-first=ls-first-first; ls-first=temp; ls-hlink=temp; /将elem2插到表中元素elem1后 void GList:setNext(GList *elem1,GList *elem2) GList *temp; while(elem1-

15、first!=NULL) temp=elem1-first; elem2-first=temp-first; delete temp; elem1-first=elem2; /将x定义为由ls指示的广义表的尾 void GList:setTail(GList * temp-utype=x.utype; if(x.utype=INTGR) temp-intinfo=x.intinfo; if(x.utype=CH r=ls; while(!(r-charinfo!=)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=

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号