版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
4/30/2026数据结构与程序设计1数据结构与程序设计(14)王丽苹lipingwang@4/30/2026数据结构与程序设计2第六章ListsandStrings
列表和字符串Restrictedlists,likestacksandqueues,inwhichchangesonlyattheendsofit.Lists,inwhichinsertions,deletions,andretrievalsmayoccuratanypointofit.Strings,是特殊的List,表示字符的集合。4/30/2026数据结构与程序设计3ListDefinitionP213AlistofelementsoftypeTisafinitesequenceofelementsofTtogetherwiththefollowingoperations: 1.Constructthelist,leavingitempty. 2.Determinewhetherthelistisempty
ornot. 3.Determinewhetherthelistisfullornot. 4.Findthesize
ofthelist. 5.Clear
thelisttomakeitempty. 6.Insert
anentryataspecifiedpositionofthelist. 7.Remove
anentryfromaspecifiedpositioninthelist. 8.Retrieve
theentryfromaspecifiedpositioninthelist. 9.Replace
theentryataspecifiedpositioninthelist. 10.Traverse
thelist,performingagivenoperationoneachentry.4/30/2026数据结构与程序设计4ImplementationofListList的有两种实现方式:顺序实现链式实现目录List下例程:为List的顺序实现方式。4/30/2026数据结构与程序设计5ImplementationofListP219enumError_code{underflow,overflow,range_error,success};constintmax_list=30;template<classList_entry>classList{public://methodsoftheListADT List(); intsize()const; boolfull()const; boolempty()const; voidclear(); voidtraverse(void(*visit)(List_entry&));//遍历整个List Error_coderetrieve(intposition,List_entry&x)const; Error_codereplace(intposition,constList_entry&x); Error_coderemove(intposition,List_entry&x); Error_codeinsert(intposition,constList_entry&x);protected://datamembersforacontiguouslistimplementation intcount; List_entryentry[max_list];};4/30/2026数据结构与程序设计6ImplementationofListtemplate<classList_entry>List<List_entry>::List()/*Post:TheListhasbeencreatedandisinitializedtobeempty.*/{ count=0;}4/30/2026数据结构与程序设计7ImplementationofListtemplate<classList_entry>intList<List_entry>::size()const/*Post:ThefunctionreturnsthenumberofentriesintheList.*/{ returncount;}4/30/2026数据结构与程序设计8ImplementationofListtemplate<classList_entry>boolList<List_entry>::full()const/*Post:ThefunctionreturnstrueorfalseaccordingtowhethertheListisfullornot.*/{ return(count==max_list);}4/30/2026数据结构与程序设计9ImplementationofListtemplate<classList_entry>boolList<List_entry>::empty()const/*Post:ThefunctionreturnstrueorfalseaccordingtowhethertheListisemptyornot.*/{returncount==0;}4/30/2026数据结构与程序设计10ImplementationofListtemplate<classList_entry>voidList<List_entry>::clear()/*Post:AllListentrieshavebeenremoved;theListisempty.*/{count=0;}4/30/2026数据结构与程序设计11ImplementationofListtemplate<classList_entry>Error_codeList<List_entry>::insert(intposition,constList_entry&x)/*Post:IftheListisnotfulland0<=position<=n;wherenisthenumberofentriesintheList,thefunctionsucceeds:Anyentryformerlyatpositionandalllaterentrieshavetheirpositionnumbersincreasedby1andxisinsertedatpositionoftheList.Else:Thefunctionfailswithadiagnosticerrorcode.*/{ if(full())returnoverflow; if(position<0||position>count)returnrange_error; for(inti=count-1;i>=position;i--)entry[i+1]=entry[i]; entry[position]=x; count++; returnsuccess;}4/30/2026数据结构与程序设计12ImplementationofListtemplate<classList_entry>Error_codeList<List_entry>::remove(intposition,List_entry&x)/*Post:If0<=position<n,wherenisthenumberofentriesintheList,thefunctionsucceeds:TheentryatpositionisremovedfromtheList,andalllaterentrieshavetheirpositionnumbersdecreasedby1.Theparameterxrecordsacopyoftheentryformerlyatposition.Else:Thefunctionfailswithadiagnosticerrorcode.*/{ if(empty())returnunderflow; if(position<0||position>=count)returnrange_error; x=entry[position]; for(inti=position;i<count-1;i++)entry[i]=entry[i+1]; count--; returnsuccess;}4/30/2026数据结构与程序设计13ImplementationofListtemplate<classList_entry>Error_codeList<List_entry>::replace(intposition,constList_entry&x)/*Post:If0<=position<n,wherenisthenumberofentriesintheList,thefunctionsucceeds:Theentryatpositionisreplacedbyx;allotherentriesremainunchanged.Else:Thefunctionfailswithadiagnosticerrorcode.*/{ if(position<0||position>=count)returnrange_error; entry[position]=x; returnsuccess;}4/30/2026数据结构与程序设计14ImplementationofListtemplate<classList_entry>Error_codeList<List_entry>::retrieve(intposition,List_entry&x)const/*Post:If0<=position<n,wherenisthenumberofentriesintheList,thefunctionsucceeds:Theentryatpositioniscopiedtox;allListentriesremainunchanged.Else:Thefunctionfailswithadiagnosticerrorcode.*/{ if(position<0||position>=count)returnrange_error; x=entry[position]; returnsuccess;}4/30/2026数据结构与程序设计15补充:指向函数的指针定义格式为:函数返回值类型(*指针变量)(形参列表);函数存放在内存的代码区域内,它们同样有地址,我们如何能获得函数的地址呢?如果我们有一个inttest(inta)的函数,那么,它的地址就是函数的名字,这一点如同数组一样,数组的名字就是数组的起始地址。定义一个指向函数的指针用如下的形式,以上面的test()为例:int(*fp)(inta);//这里就定义了一个指向函数的指针函数指针不能指向不同类型,或者是带不同参数的函数。指向函数的指针常用于函数的形参中4/30/2026数据结构与程序设计16#include<iostream>#include<string>usingnamespacestd;
inttest(inta);
voidmain(){cout<<“函数地址:”<<test<<endl;//显示函数入口地址
int(*fp)(inta);//定义了一个指向函数的指针fpfp=test;//将函数test的地址赋给函数指针fpcout<<fp(5)<<endl;//输出fp(5),这是标准c++的写法,cout<<(*fp)(10)<<endl;//(*fp)(10)这是兼容c语言的标准写法,//两种写法的功能相同,但注意区分,避免写的程序产生移植性问题!}
inttest(inta){returna;}4/30/2026数据结构与程序设计17ImplementationofListtemplate<classList_entry>voidList<List_entry>::traverse(
void(*visit)(List_entry&))/*Post:Theactionspecifiedbyfunction(*visit)hasbeenperformedoneveryentryoftheList,beginningatposition0anddoingeachinturn.*/{ for(inti=0;i<count;i++)(*visit)(entry[i]);}4/30/2026数据结构与程序设计18ImplementationofListtemplate<classList_entry>voidupdate(List_entry&x){ x*=2; }4/30/2026数据结构与程序设计19ImplementationofListtemplate<classList_entry>voidprint(List_entry&x){ cout<<x<<endl; }4/30/2026数据结构与程序设计20ImplementationofListvoidmain(){ List<int>mylist; for(int
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 教师招聘生物学科知识试题及答案
- 一例电击伤患者的护理个案
- 打印复印设备维护保养计划方案
- 一例肺癌化疗患者的护理个案
- 初中英语学科知识与教学能力试题及答案
- 会议管理制度试卷及答案
- 沙尘暴应急培训
- 冠状动脉支架后并发冠状动脉血栓栓塞护理查房
- 酮加氢合成醇项目可行性研究报告模板立项申批备案
- 无备案消毒产品市场清查
- 小学一年级劳动课教案(全册)
- 企业微信的使用培训
- 2025年语文四年级下第二单元习作范文10篇(我的奇思妙想)
- 三星PL170-PL171数码相机(中文)说明书
- 电气工程及其自动化专业导论
- GA/T 761-2024停车库(场)安全管理系统技术要求
- 部编版历史八年级下册小论文(20篇)(学案)
- 大学生创新创业基础(创新创业课程)完整全套教学课件
- DL∕T 1069-2016 架空输电线路导地线补修导则
- 卫生院财务培训课件
- 宜家IWAY-执行标准
评论
0/150
提交评论