版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Linked list Data StructureSoftware College Northeastern University问题:在一个有序表中插入一个数据元素 Data StructureSoftware College Northeastern UniversitySequence List Solutionstemplate class SeqList Type *data; int MaxSize; int last; /index of the last elementpublic: SeqList ( int MaxSize = defaultSize ); SeqList
2、 ( ) delete data; int Length ( ) const return last+1; int Find ( Type & x ) const; Data StructureSoftware College Northeastern UniversityProgramming Exercise int SeqList:Insert_sorted(Type value) 插入成功时返回0 表内有相同的数据元素时插入失败返回1Data StructureSoftware College Northeastern Universitytemplate int SeqList:In
3、sert_sorted( Type value) i=0; while(i = last & datai value) i+; if( ii-1;j-) dataj+1=dataj; datai= value; last+; return 0;Data StructureSoftware College Northeastern UniversityProgramming Exercise int List:Insert_sorted(Type value) 插入成功时返回0 表内有相同的数据元素时插入失败返回1Data StructureSoftware College Northeaste
4、rn UniversityTemplate of linked list(1)template class List;template class ListNode friend class List; Type data; /结点数据 ListNode *link; /结点链接指针public: ListNode ( ); /链表结点构造函数 ListNode ( const Type& item ); ListNode *NextNode ( ) return link; /给出当前结点的下一结点地址Data StructureSoftware College Northeastern U
5、niversityvoid InsertAfter ( ListNode *p ); /在当前结点后插入结点p ListNode *RemoveAfter ( ); /摘下当前结点的下一结点;template class List ListNode *first, *last;public: ListNode *GetNode ( const Type& item, ListNode *next ); /创建数据为item,指针为next的新结点Template of linked list(2)Data StructureSoftware College Northeastern Unive
6、rsity List ( const Type & value ) last =first = new ListNode( value ); /构造函数 List ( ); /析构函数 void MakeEmpty ( ); /链表置空 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 ); int Insert_sorte
7、d(Type value); Template of linked list(3)Data StructureSoftware College Northeastern UniversityLinked List SolutionsFirst1234Prep1、从前向后查找大于等于待插入数据元素的位置2、如果有相同数据元素,返回1 否则在该位置之前插入新数据元素,返回0Data StructureSoftware College Northeastern Universitytemplate int List:Insert_sorted( Type value) ListNode *pre =
8、first,*p=first-link; while(p != NULL & p-data link; if( p != NULL & p-data = value) return 1; q = new ListNode( value ); q-link = pre-link; pre-link = q; return 0;Data StructureSoftware College Northeastern UniversityProgramming Exercise int List:If_sorted() 如果是有序的返回0 如果是无序的返回1Data StructureSoftware
9、 College Northeastern UniversityLinked List SolutionsFirst1234pre借助冒泡排序的思想 两两相邻的结点中存储的数据进行比较 一旦发现不是由小到大的,则判断链表是无序的问题解决的关键: 相邻的结点的获取Data StructureSoftware College Northeastern Universitytemplate int List:If_sorted( Type value) ListNode *pre =first-link,*p; while(pre != NULL) p = pre-link; if(p = NULL
10、 ) return 0; else if (pre-data p-data) return 1; else pre = p; return 0;Data StructureSoftware College Northeastern UniversityProgramming Exercise void List:Delete_Same() 链表中的数据元素按照元素值由大到小存储,去掉表中数值相同的数据元素Data StructureSoftware College Northeastern UniversityLinked List SolutionsFirst5533p对链表上的每一个元素结
11、点 检查后面相邻的结点是否和其值相同, 如果相同删除其后面的结点Data StructureSoftware College Northeastern Universitytemplate void List:delete_same( Type value) ListNode *p =first-link,q; while(p != NULL) q = p-link; while(q != NULL & p-data = q-data) p-link = q-link; delete q; q = p-link; p = p-link; return;Data StructureSoftwar
12、e College Northeastern Universitytemplate class DblList;template class DblNode friend class DblList;private: Type data; int freq; /数据 DblNode *lLink, *rLink; /指针 DblNode ( Type value, /构造函数 DblNode *left, DblNode *right ) : data (value), lLink (left), rLink (right) Doubly linked lists(1)Data Structu
13、reSoftware College Northeastern University DblNode ( Type value ) : data (value), lLink (NULL), rLink (NULL) ;template class DblList public: DblList ( Type uniqueVal ); DblList ( ); int Length ( ) const; int IsEmpty ( ) return firstrlink = first; int Find ( const Type & target ); Type getData ( ) co
14、nst;Doubly linked lists(2)Data StructureSoftware College Northeastern University void Firster ( ) current = first; int First ( ); int Next ( ); int Prior ( ); int operator ! ( ) return current != NULL; void Insert ( const Type & value ); void Remove ( );private: DblNode *first, *current;Implementati
15、on of Doubly linked lists(3)Data StructureSoftware College Northeastern UniversityDoubly linked lists:SearchingData StructureSoftware College Northeastern UniversityProgramming Exercise DblNode * DblList :Locate(Type value) 双向链表中数据元素按照访问频度由大到小进行排序 获取元素在双向链表中的位置 如果不存在,返回空 如果存在, 把访问频度加1,并按照访问频度对结点调整位置
16、 (查找插入位置,删除结点,插入结点)Data StructureSoftware College Northeastern UniversityProgramming Exercise DblNode * DblList :Locate(Type value) 过程: (1)查找元素位置 (2)根据查找结果处理 不存在 存在,频度加1,判断是否需要移动 如需要移动,逆向查找合适的移动位置Data StructureSoftware College Northeastern Universitytemplate DblNode * DblList :Locate(Type value) DblNode *p =first-right,; while(p != first & p-data != value) p = p-right; if(p = first) return NULL; else p-freq+; pre = p-left; while(pre != first & pre-freq freq) pre=pre-left;Data Struc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基因鉴定入门初级工作者的工作计划与实施
- 职业健身教练初级培训与教学计划
- 验光师在视疲劳患者管理中的服务计划
- 宠物针灸AI算法初级技术学习计划
- 幼儿绿植种植通知书
- 广东省督办通知书
- 广州房东租房涨价通知书
- 延吉小营镇停电通知书
- 建平职高放假通知书
- 开平小区封控通知书
- 智慧校园建设“十五五”发展规划
- 2025年研究生入学考试《管理类联考》试卷真题及完整解析
- 2025至2030年中国智能炒菜机(炒菜机器人)行业市场现状调查及前景战略研判报告
- 2025至2030年中国聚乙烯蜡行业市场运行态势及发展前景研究报告
- YZ电信装维服务质量的SERVQUAL模型改进
- 《体重管理指导原则(2024年版)》解读 课件
- 玉米绿色防控技术课件
- DB11∕T 387.1-2016 水利工程施工质量评定 第1部分:河道整治
- 销售行业转正自我介绍
- 体艺教育特色学校汇报
- 人教版四年级上册《道德与法治》教案
评论
0/150
提交评论