




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/*使用循环双向链表,实现名胜信息管理系统,可实现插入,删除,查询,文件读取,存储,有序归并,输出的功能 */ #include #include #include using namespace std; class cscene /景点类 private: string name; /景点名string city; /所在城市float ticket; /票价friend class cscenenode; /结点友元类friend class cscenelist; /链表友元类friend class csceneview; /视图友元类public: cscene():name(un
2、known),city(unknown),ticket(0) /构造函数cscene(cscene &s) /拷贝构造函数 name=; city=s.city; ticket=s.ticket; cscene(string n,string c,float t) / 构造函数 name=n; city=c; ticket=t; void outputscene() /输出函数 coutnametcitytticketendl; cout-endl; void input() /输入 cout 请依次输入名胜名称,所在城市,票价namecityticket; ; class
3、 cscenenode / 结点类 private: head length a1 a2 a3 cscene scene; /内嵌对象cscenenode *prior; /指向前驱cscenenode *next; /指向后继friend class cscenelist; /链表友元类friend class csceneview; /视图友元类public: cscenenode():next(null),prior(null),scene() /构造函数cscenenode(cscene s):next(null),prior(null),scene(s) /构造函数; class c
4、scenelist /链表类 private: string listname; /链表名int length; / 表长cscenenode *head; /头指针friend class csceneview; /视图友元类public: cscenelist():listname(未分类 ),length(0) /构造函数 head=new cscenenode; head-prior=head; head-next=head; cscenelist() /析构函数 cscenenode *p,*q; for(p=head-next;p!=head;p=q) q=p-next; dele
5、te p; delete head; head=null; void output() /输出全部 cout-endl; coutlistname 全部景点 endl; cout-next,j=1;p!=head;p=p-next,j+) tscene.citytscene.ticketendl; cout-endl; cout 总数lengthendl; bool inserthead(cscene scene) /表头插入,可用任意位置插入函数代替其功能 cscenenode *pnew=new cscenenode(scene); /申请新结点if(!pn
6、ew) cout 内存申请失败,插入失败!prior=head; /修改新结点pnew-next=head-next; /修改新结点head-next-prior=pnew; /修改邻近结点head-next=pnew; /修改头结点length+; /表长加一coutinserted .endl; return true; bool inserttail(cscene scene) /表尾插入 cscenenode *pnew=new cscenenode(scene); /申请新结点if(!pnew) cout 内存申请失败,插入失败!next=head; /修改新结点pnew-prior
7、=head-prior; /修改新结点head-prior-next=pnew; /修改原末结点head-prior=pnew; /修改头结点length+; /表长加一return true; bool insert(cscene scene,int i) / 第 i 位置插入 if(i(length+1) /i越界 a pnew head length a1 a2 a3 第 i位置插入函数图解cout 指定位置非法! endl; return false; cscenenode *pnew=new cscenenode(scene); /申请新结点if(!pnew) cout 内存申请失败
8、,插入失败!next,j=1;jnext,j+); /寻找第 i 个结点pnew-prior=p-prior; /修改新结点pnew-next=p; /修改新结点p-prior-next=pnew; /修改前邻近结点p-prior=pnew; /修改后邻近结点length+; /表长加一coutinserted .endl; return true; bool remove(int i) /删除第 i 个元素 if(ilength) /i 越界 cout 指定位置非法 ,删除失败! next,j=1;jnext,j+); /寻找第 i 个结点p-prior-next=p-next; /修改被删
9、元素前邻近结点p-next-prior=p-prior; /修改被删元素后邻近结点delete p; /删除元素 coutremoved.next;p!=head;p=q) q=p-next; delete p; p-next=head; p-prior=head; listname= 未分类 ; length=0; return true; void find(string name) /按名称查找 for(cscenenode *p=head-next;p!=head;p=p-next) if(=name) p-scene.outputscene(); cscene
10、node& operator(int i) /获取元素 i+; if(ilength) /i 越界 cout 指定位置非法! next;jnext,j+); /寻找第 i 个元素 p return *p; bool merge(cscenelist &lista,cscenelist &listb) /有序归并,按票价升序排列 clear(); /清空原有元素cscenenode *pa=lista.head-next; /指向 lista 待插元素cscenenode *pb=listb.head-next; /指向 listb 待插元素cscenenode *pc=
11、head; /指向待插位置前驱while(pa!=lista.head)&(pb!=listb.head) / if(pa-scene.ticketscene.ticket) pc-next=pa; pa-prior=pc; pc=pa; pa=pa-next; else pc-next=pb; pb-prior=pc; pc=pb; pb=pb-next; pc-next=(pa!=lista.head)?pa:pb; / 连接未插链表head-prior=(pa!=lista.head)?(lista.head -prior):(listb.head-prior); /修改头结点h
12、ead-prior-next=head; / 修改尾结点length=lista.length+listb.length; / 计算新表表长lista.head -prior=lista.head; / 将 a 表置空lista.head-next=lista.head; lista.length =0; listb.head -prior=listb.head; / 将 b 表置空listb.head-next=listb.head; listb.length =0; return true; bool read(string filename) /从文件读入数据 ifstream file
13、(filename.c_str(); if(!file) pa 有序归并函数图解pb head length a1 a2 a3 listb head length a1 a2 a3 lista pc a head length cout 文件打开失败! listname; /读取链表名filelength; /读取表长信息for(int i=1;scene.cityscene.ticket; inserttail(scene); return true; bool write(string filename) /输出数据到文件 ofstream file(filename
14、.c_str(); if(!file) cout 文件保存失败! endl; return false; filelistnameendl; /保存链表名filelengthnext;p!=head;p=p-next) /保存景点信息 tscene.citytscene.ticketendl; file.close(); return true; ; class csceneview / 视图类 private: bool modified; /数据是否发生修改的状态标志string filename; /文件名cscenelist list1; /内嵌链表对象 p
15、ublic: csceneview() : modified(false),filename(),list1() /构造函数bool savemodified() /判断、提示是否需要保存数据 if(!modified) return false; while(1) cout c; switch(c) case y: case y: return true; case n: case n: return false; void save() /保存数据到指定文件 if(!filename.length() /判断文件名是否为空 cout filename; else if(!modified)
16、 return; list1.write(filename); modified = false; void open() /指定文件名,读取数据 cout filename; if(!list1.read(filename) cout !打开文件失败 ! endl; modified = false; void commandcycle() /菜单函数 ,显示菜单,输入并解析指令 int command=1; /指令int i; / 用于表示操作元素位序和子指令string name; /名胜名或文件名,用于查找和加载cscene scene1; /景点类对象cscenelist lista
17、,listb; /链表类对象,用于归并暂存cout*endl; cout 名胜管信息管理系统endl; cout*endl; while(command!=0) cout 请选择操作: 1.插入 2.查询 3.删除 4.输出所有 5.有序归并 6.保存 7.打开 0.退出 command; switch(command) case 1: scene1.input(); couti; if(list1.insert(scene1,i) modified = true; break; case 2: coutname; cout查找结果如下 endl; list1.find(name); break; case 3: couti; if(list1.remove(i) modified = true; break; case 4: list1.output(); break; case 5: cout 请依次输入要归并的有序表的文件名:a、bendl; coutname; lista.read(name); coutname; listb.read(name); if(savemodified() save(); list1.merge(l
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国水性氟碳涂料行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国氟化铝钾行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国橡塑密封件行业发展分析及投资风险预测研究报告
- 2025-2030年中国椭圆形书桌行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国枪式话筒行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国机场旅客登机桥行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国木耳行业发展分析及投资风险预测研究报告
- 2025-2030年中国有机排毒产品行业市场现状供需分析及投资评估规划分析研究报告
- 2025-2030年中国暖通空调空气质量传感器行业市场现状供需分析及投资评估规划分析研究报告
- 护理职业发展规划试题及答案提示
- 2025年中国邮政集团工作人员招聘考试笔试试题(含答案)
- 大部分分校:地域文化形考任务一-国开(CQ)-国开期末复习资料
- GB 21670-2008乘用车制动系统技术要求及试验方法
- 中央空调的PLC控制
- 公立医院内部控制管理办法课件
- 投标报名登记表格式
- DB4403-T 87-2020 园林绿化管养规范-(高清现行)
- 人教版(2019) 选择性必修第四册 Unit 1 Science Fiction 读后续写学案
- 屋面防水维修工程施工组织设计
- 糖尿病的并发症1
- 超星尔雅学习通《现场生命急救知识与技能》章节测试含答案
评论
0/150
提交评论