数据结构英文教学课件:chapter4 Array and Linked list exercise_第1页
数据结构英文教学课件:chapter4 Array and Linked list exercise_第2页
数据结构英文教学课件:chapter4 Array and Linked list exercise_第3页
数据结构英文教学课件:chapter4 Array and Linked list exercise_第4页
数据结构英文教学课件:chapter4 Array and Linked list exercise_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论