版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Data StructureSoftware College Northeastern UniversityArray and Linked listData StructureSoftware College Northeastern UniversityProgramming Exercise Reverse a list List:Reverse()if L contains three nodes with data 1, 2, and 3 (read in order from the head of the list to the tail) then the Reverse li
2、st that is returned by the function should also contain three nodes with data 3, 2, and 1 (read in order from the head of the list).Data StructureSoftware College Northeastern UniversitySequence List Solutionstemplate class SeqList Type *data; int MaxSize; int last; /index of the last elementpublic:
3、 SeqList ( int MaxSize = defaultSize ); SeqList ( ) delete data; int Length ( ) const return last+1; int Find ( Type & x ) const; void Reverse(); Data StructureSoftware College Northeastern UniversitySequence List Solutions12345140124lastData StructureSoftware College Northeastern UniversitySequence
4、 List Solutions1234514012454321140124tempData StructureSoftware College Northeastern UniversitySequence List Solutions5432114012454321140124tempData StructureSoftware College Northeastern University template void SeqList:Reverse() /Reverse the list Type *temp; temp = new Typelast+1; if(temp != NULL)
5、 for(int i=0,j=last;i=last;i+,j-) *(temp+i)=*(data+j) for(int i=0;i=last;i+) *(data+i)=*(temp+j) delete temp; Data StructureSoftware College Northeastern UniversitySequence List Solutions12345140124Data StructureSoftware College Northeastern University template void SeqList:Reverse() /Reverse the li
6、st int halflength = (last+1)/2; Type temp; for(int i=0;j=last;ihalflength;i+,j-) temp = *(data+i); *(data+i) = *(data+j); *(data+j) = temp; Data StructureSoftware College Northeastern UniversityTemplate of linked list(1)template class List;template class ListNode friend class List; Type data; /结点数据
7、ListNode *link; /结点链接指针public: ListNode ( ); /链表结点构造函数 ListNode ( const Type& item ); ListNode *NextNode ( ) return link; /给出当前结点的下一结点地址Data StructureSoftware College Northeastern Universityvoid InsertAfter ( ListNode *p ); /在当前结点后插入结点p ListNode *RemoveAfter ( ); /摘下当前结点的下一结点;template class List Lis
8、tNode *first, *last;public: ListNode *GetNode ( const Type& item, ListNode *next ); /创建数据为item,指针为next的新结点Template of linked list(2)Data StructureSoftware College Northeastern University List ( const Type & value ) last =first = new ListNode( value ); /构造函数 List ( ); /析构函数 void MakeEmpty ( ); /链表置空
9、int Length ( ) const; /求链表长度 ListNode *Find ( Type value ); ListNode *Find ( int i ); int Insert ( Type value, int i ); Type *Remove ( int i ); Type *Get ( int i ); void Reverse(); Template of linked list(3)Data StructureSoftware College Northeastern UniversityLinked List Solutions 15FirstLastData S
10、tructureSoftware College Northeastern UniversityInsert Before the first element 15FirstLastPData StructureSoftware College Northeastern UniversityLinked List Solutions 25FirstLastP1Data StructureSoftware College Northeastern UniversityLinked List Solutions 35FirstP21Data StructureSoftware College Northeastern UniversityLinked List Solutions 45FirstP321Data StructureSoftware College Northeastern UniversityLinked List Solutions 5FirstP4321Data StructureSoftware College Northeastern UniversityLinked List SolutionsFirstNULLP54321Data StructureSoftware College Northeastern Universitytempl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商铺租赁监控安装协议2025
- 商铺租赁保险协议2025
- 商铺转租合同协议2025年规范
- 人力资源招聘协议2025年规范版
- 品牌推广外包协议2025
- 2025年员工内部奖金制度考试试题及答案
- 客服兼职合同协议2025年标准
- 健身俱乐部会员协议2025年
- 回填沙土出售合同范本
- 墙面翻新质保合同范本
- DCS控制系统管理制度
- 8d报告资料考试卷及答案
- T-CNEA 019-2022 核电工程设备单体调试范围及管理通.用要求
- 新生儿早产儿个案护理
- 农药安全评价体系-深度研究
- 全国青少年文化遗产知识大赛(小学组)参考试题库(含答案)
- 河南省信阳市2024-2025学年普通高中高三第二次教学质量检测生物模拟试题(含答案)
- 2024年砌筑工(高级技师)技能鉴定理论考试题库(含答案)
- 2024年初级招标采购从业人员《招标采购法律法规》考前必刷必练题库500题(含真题、必会题)
- 2024年北京某中学初一(上)期中语文试题及答案
- 道路改造施工方案
评论
0/150
提交评论