




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 定义: The insertions,deletions and retrieval can occured at any point of the list.2、 实现:*utility.h:#include #include #include #include #include enum Error_code success, fail, range_error, underflow, overflow, fatal, not_present, duplicate_error, entry_inserted, entry_found, internal_error ;顺序实现:*list.h:#include utility.hconst int max_list = 1000; template class List public:/ methods of the List ADT List(); int size() const; bool full() const; bool empty() const; void clear(); void traverse(void (*visit)(List_entry &); Error_code retrieve(int position, List_entry &x) const; Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x); Error_code insert(int position, const List_entry &x);protected:/ data members for a contiguous list implementation int count; List_entry entrymax_list;template List:List() count = 0;template void List:clear() count = 0;template int List:size() const return count;template bool List:empty() const return count = 0;template bool List:full() const return count = max_list;template void List:traverse(void (*visit)(List_entry &) for (int i = 0; i count; i+) (*visit)(entryi);template Error_code List:insert(int position, const List_entry &x) if (full() return overflow; if (position count) return range_error; for (int i = count - 1; i = position; i-) entryi + 1 = entryi; entryposition = x; count+; return success;template Error_code List:retrieve(int position, List_entry &x) const if (position = count) return range_error; x = entryposition; return success;template Error_code List:replace(int position, const List_entry &x) if (position = count) return range_error; entryposition = x; return success;template Error_code List:remove(int position, List_entry &x) if (count = 0) return underflow; if (position = count) return range_error; x = entryposition; count-; while (position count) entryposition = entryposition + 1; position+; return success;链式实现:*node.h:template struct Node / data members Node_entry entry; Node *next;/ constructors Node(); Node(Node_entry, Node *link = NULL);template Node:Node() next = NULL;template Node:Node (List_entry data, Node *link) entry = data; next = link;*list.h:#include utility.h#include node.htemplate class List public:/ Specifications for the methods of the list ADT go here. List(); int size() const; bool full() const; bool empty() const; void clear(); void traverse(void (*visit)(List_entry &); Error_code retrieve(int position, List_entry &x) const; Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x); Error_code insert(int position, const List_entry &x);/ The following methods replace compiler-generated defaults. List(); List(const List ©); void operator =(const List ©);protected:/ Data members for the linked list implementation now follow. int count; Node *head;/ The following auxiliary function is used to locate list positions Node *set_position(int position) const;template List:List() count = 0; head = NULL;template void List:clear() Node *p, *q; for (p = head; p; p = q) q = p-next; delete p; count = 0; head = NULL;template int List:size() const return count;template bool List:empty() const return count = 0;template bool List:full() const return false;template void List:traverse(void (*visit)(List_entry &) Node *q; for (q = head; q; q = q-next) (*visit)(q-entry);template Error_code List:insert(int position, const List_entry &x) if (position count) return range_error; Node *new_node, *previous, *following; if (position 0) previous = set_position(position - 1); following = previous-next; else following = head; new_node = new Node(x, following); if (new_node = NULL) return overflow; if (position = 0) head = new_node; else previous-next = new_node; count+; return success;template Error_code List:retrieve(int position, List_entry &x) const Node *current; if (position = count) return range_error; current = set_position(position); x = current-entry; return success;template Error_code List:replace(int position, const List_entry &x) Node *current; if (position = count) return range_error; current = set_position(position); current-entry = x; return success;template Error_code List:remove(int position, List_entry &x) Node *prior, *current; if (count = 0) return fail; if (position = count) return range_error; if (position 0) prior = set_position(position - 1); current = prior-next; prior-next = current-next; else current = head; head = head-next; x = current-entry; delete current; count-; return success;template Node *List:set_position(int position) const Node *q = head; for (int i = 0; i next; return q;template List:List() clear();template List:List(const List ©) count = copy.count; Node *new_node, *old_node = copy.head; if (old_node = NULL) head = NULL; else new_node = head = new Node(old_node-entry); while (old_node-next != NULL) old_node = old_node-next; new_n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版西北旺外墙改造项目质量保障及施工合同
- 2025年度事业单位员工退休待遇补充合同
- 2025保险代理咨询服务合同模板(含保险科技)
- 河北省昌黎县2025年上半年公开招聘村务工作者试题含答案分析
- 2025年度光伏产品代理进口合作协议
- 海南省万宁市2025年上半年公开招聘村务工作者试题含答案分析
- 2025电脑包年维护合同含硬件更换与故障响应服务
- 海南省澄迈县2025年上半年公开招聘城市协管员试题含答案分析
- 2025版石材幕墙安装与工程款支付进度合同
- 2025年度拆迁安置房买卖及物业管理合同
- 商会2025上半年工作总结及下半年工作计划
- 《城市河湖底泥污染状况调查评价技术导则》
- 《CIP培训资料》课件
- 无人机飞行安全知识讲座
- 物业楼宇管家岗位培训
- 土木公司大直径双曲线冷却塔施工技术交流
- 数字孪生应用技术员职业技能竞赛题及答案
- Unit 5 Reveling nature Understanding ideas A journey of Discovery教学设计 2023-2024学年外研版(2019)高中英语选择性必修第一册
- 剪映操作全教程
- 人教版(2024新版)七年级上册英语全册教案
- 湖北省2025届高三(9月)起点考试 英语试卷(含答案)
评论
0/150
提交评论