版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构与程序设计(18)王丽苹lipingwang@4/29/20261数据结构与程序设计Chapter7SEARCHING
Informationretrievalisoneoftheimportantapplicationsofcomputers.Wearegivenalistofrecords,whereeachrecordisassociatedwithonepieceofinformation,whichweshallcallakey(关键字).(BOOKP269FIGURE7.1)本章讨论顺序存储列表的查找操作,链接存储在第10章讨论。4/29/20262数据结构与程序设计Chapter7SEARCHINGWearegivenonekey,calledthetarget,andareaskedtosearchthelisttofindtherecord(s)(ifany)whosekeyisthesameasthetarget.Weoftenaskhowtimesonekeyiscomparedwithanotherduringasearch.Thisgivesusagoodmeasureofthetotalamountofworkthatthealgorithmwilldo.4/29/20263数据结构与程序设计Chapter7SEARCHING
Thesearchingproblemfallsnaturallyintotwocases.Internalsearching(内部查找)meansthatalltherecordsarekeptinhigh-speedmemory.Inexternalsearching(外部查找),mostoftherecordsarekeptindiskfiles.Westudyonlyinternalsearching.4/29/20264数据结构与程序设计ImplementationofKeyClassclassKey{
intkey;public: Key(intx=0);
int
the_key()const;};booloperator==(constKey&x,constKey&y);4/29/20265数据结构与程序设计ImplementationofKeyClassKey::Key(intx){ key=x;}int
Key::the_key()const{ returnkey;}booloperator==(constKey&x,constKey&y){ returnx.the_key()==y.the_key();}4/29/20266数据结构与程序设计ImplementationofRecordClassclassRecord{public:
operatorKey();//implicitconversionfrom RecordtoKey.
Record(intx=0,inty=0);private:
intkey;
intother;};4/29/20267数据结构与程序设计ImplementationofRecordClassRecord::Record(intx,inty){ key=x; other=y;}Record::operatorKey(){ Keytmp(key);
returntmp;}4/29/20268数据结构与程序设计TestMainvoidmain(){ Keytarget(5); Recordmyrecord(5,9);
if(target==myrecord)
cout<<"yes"<<endl; elsecout<<"no"<<endl;}调用operatorKey()构造临时Keytmp,采用voidoperator==(constKey&x,constKey&y)操作符比较。Output:yes4/29/20269数据结构与程序设计ImplementationofSearch目录SeqSearch下例程4/29/202610数据结构与程序设计AnotherMethodRecorder中的成员operatorKey();主要完成从Recorder对象到Key对象的自动转换。可以用其他方法完成该功能。Useconstructortoconversionfrom
RecordtoKey.4/29/202611数据结构与程序设计ImplementationofKeyClassclassKey{
intkey;public: Key(intx=0);
Key(constRecord&r);
int
the_key()const;};booloperator==(constKey&x,constKey&y);4/29/202612数据结构与程序设计ImplementationofKeyClassKey::Key(intx){ key=x;}Key::Key(constRecord&r){ key=r.the_key();}int
Key::the_key()const{ returnkey;}booloperator==(constKey&x,constKey&y){ returnx.the_key()==y.the_key();}4/29/202613数据结构与程序设计ImplementationofRecordClassclassRecord{public:
Record(intx=0,inty=0);
int
the_key()const;private:
intkey;
intother;};4/29/202614数据结构与程序设计ImplementationofRecordClassRecord::Record(intx,inty){ key=x; other=y;}int
Record::the_key()const{ returnkey;}4/29/202615数据结构与程序设计TestMainvoidmain(){ Keytarget(5); Recordmyrecord(5,9);
if(target==myrecord)
cout<<"yes"<<endl; elsecout<<"no"<<endl;}调用Key(constRecord&r)构造临时Keytmp,采用voidoperator==(constKey&x,constKey&y)操作符比较后,调用析构函数释放tmp。Output:yes4/29/202616数据结构与程序设计ImplementationofSearch目录SeqSearch2下例程4/29/202617数据结构与程序设计SequentialSearch顺序查找P272Error_code
sequential_search(constList<Record>&the_list,constKey&target,int&position)/*Post:Ifanentryinthelisthaskeyequaltotarget,thenreturnsuccessandtheoutputparameterpositionlocatessuchanentrywithinthelist.Otherwisereturnnot_presentandpositionbecomesinvalid.*/{ints=the_list.size();for(position=0;position<s;position++){Recorddata;
the_list.retrieve(position,data);if(target==data)returnsuccess;}returnnot_present;}4/29/202618数据结构与程序设计AnalysisBOOKP273Thenumberofcomparisonsofkeysdoneinsequentialsearchofalistoflengthnis:Unsuccessfulsearch:ncomparisons.Successfulsearch,bestcase:1comparison.Successfulsearch,worstcase:ncomparisons.Successfulsearch,averagecase:(n+1)/2comparisons.4/29/202619数据结构与程序设计BinarySearchSequentialsearchiseasytowriteandefficientforshortlists,butadisasterforlongones.Oneofthebestmethodsforalistwithkeysinorderisbinarysearch.首先讨论如何构建一个有序的列表。然后讨论针对顺序列表的二分查找。4/29/202620数据结构与程序设计OrderedLists有序列表的定义:DEFINITIONAnorderedlistisalistinwhicheachentrycontainsakey,suchthatthekeysareinorder.Thatis,ifentryicomesbeforeentryjinthelist,thenthekeyofentryiislessthanorequaltothekeyofentryj.4/29/202621数据结构与程序设计OrderedListsclassOrdered_list:publicList<Record>{public:
Error_code
insert(constRecord&data);
Error_code
insert(intposition,constRecord&data);
Error_code
replace(intposition,constRecord&data);};4/29/202622数据结构与程序设计OrderedListsError_code
Ordered_list::insert(constRecord&data)/*Post:IftheOrdered_listisnotfull,thefunctionsucceeds:TheRecorddataisinsertedintothelist,followingthelastentryofthelistwithastrictlylesserkey(orinthefirstlistpositionifnolistelementhasalesserkey).Else:thefunctionfailswiththediagnosticError_codeoverflow.*/4/29/202623数据结构与程序设计OrderedListsError_code
Ordered_list::insert(constRecord&data)/*Post:IftheOrdered_listisnotfull,thefunctionsucceeds:TheRecorddataisinsertedintothelist,followingthelastentryofthelistwithastrictlylesserkey(orinthefirstlistpositionifnolistelementhasalesserkey).Else:thefunctionfailswiththediagnosticError_codeoverflow.*/{
ints=size();
intposition; for(position=0;position<s;position++){ Recordlist_data;
retrieve(position,list_data);
if(data<list_data)break;//bookp279wrong } returnList<Record>::insert(position,data);//调用父类的方法。}4/29/202624数据结构与程序设计OrderedListsError_code
Ordered_list::insert(intposition,constRecord&data)/*Post:IftheOrderedlistisnotfull,0<=position<=n,wherenisthenumberofentriesinthelist,andtheRecorddatacanbeinsertedatpositioninthelist,withoutdisturbingthelistorder,thenthefunctionsucceeds:Anyentryformerlyinpositionandalllaterentrieshavetheirpositionnumbersincreasedby1anddataisinsertedatpositionoftheList.Else:thefunctionfailswithadiagnosticError_code.*/4/29/202625数据结构与程序设计OrderedListsError_code
Ordered_list::insert(intposition,constRecord&data)/*Post:succeeds:AnyentryformerlyinpositionandalllaterentrieshavetheirElse:thefunctionfailswithadiagnosticError_code.*/{ Recordlist_data; if(position>0){
retrieve(position-1,list_data); if(data<list_data) returnfail; } if(position<size()){
retrieve(position,list_data); if(data>list_data) returnfail; } returnList<Record>::insert(position,data);}4/29/202626数据结构与程序设计OrderedListsError_code
Ordered_list::replace(intposition,constRecord&data){ if(position<0||position>=count)return
range_error; Recordlist_data; if(position>0){
retrieve(position-1,list_data); if(data<list_data) returnfail; } if(position<size()-1){
retrieve(position+1,list_data); if(data>list_data) returnfail; }
entry[position]=data; returnsuccess;}4/29/202627数据结构与程序设计OrderedListsclassRecord{public: Record();
Record(intx,inty=0);
int
the_key()const;private:
intkey;
intother;};booloperator>(constRecord&x,constRecord&y);booloperator<(constRecord&x,constRecord&y);ostream&operator<<(ostream&output,Record&x);4/29/202628数据结构与程序设计OrderedListsRecord::Record(){ key=0; other=0;}Record::Record(intx,inty){ key=x; other=y;}int
Record::the_key()const{ returnkey;}4/29/202629数据结构与程序设计OrderedListsbooloperator>(constRecord&x,constRecord&y){ returnx.the_key()>y.the_key();}booloperator<(constRecord&x,constRecord&y){ returnx.the_key()<y.the_key();}ostream&operator<<(ostream&output,Record&x){ output<<x.the_key(); output<<endl; returnoutput;}4/29/202630数据结构与程序设计OrderedLists--Maintemplate<classList_entry>void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 外贸客户开发的方法与渠道
- 纳米技术在纤维织物催化中的作用
- 混合动力汽车构造原理与故障检修 教案(第五章)混合动力汽车高压配电箱检修
- 项目四 混合动力汽车电机驱动系统检修
- 企业财务风险控制流程手册
- 电力系统自动化运维与故障预警系统构建指南
- 无人机领域技术创新承诺书范文6篇
- 合作方产品质量不合格处理回复函7篇范本
- 销售策略调整实施细节方案
- 项目验收标准最终确认函(5篇)范文
- 妇幼保健机构中的患者隐私保护与母婴信息管理
- 耳鼻喉科电子喉镜检查操作规范
- 2026中国长江三峡集团有限公司春季校园招聘笔试参考题库及答案解析
- 2026年宁波报业传媒集团有限公司校园招聘笔试参考试题及答案解析
- 2026广东省三宜集团有限公司招聘19人备考题库附答案详解(综合题)
- 电瓶车销售管理制度(3篇)
- 2026年及未来5年市场数据中国量子点发光二极管(QLED) 行业市场全景分析及投资战略规划报告
- 徐工集团入职在线测评题库
- 总包变清包工合同范本
- 【《剪叉式举升机结构的优化设计》8400字】
- GB/T 33653-2025油田生产系统能耗测试和计算方法
评论
0/150
提交评论