版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/*使用循環(huán)雙向鏈表,實(shí)現(xiàn)名勝信息管理系統(tǒng),可實(shí)現(xiàn)插入,刪除,查詢,文件讀取,存儲(chǔ),有序歸并,輸出的功能 */ #include #include #include using namespace std; class cscene /景點(diǎn)類(lèi) private: string name; /景點(diǎn)名string city; /所在城市float ticket; /票價(jià)friend class cscenenode; /結(jié)點(diǎn)友元類(lèi)friend class cscenelist; /鏈表友元類(lèi)friend class csceneview; /視圖友元類(lèi)public: cscene():name(un
2、known),city(unknown),ticket(0) /構(gòu)造函數(shù)cscene(cscene &s) /拷貝構(gòu)造函數(shù) name=; city=s.city; ticket=s.ticket; cscene(string n,string c,float t) / 構(gòu)造函數(shù) name=n; city=c; ticket=t; void outputscene() /輸出函數(shù) coutnametcitytticketendl; cout-endl; void input() /輸入 cout 請(qǐng)依次輸入名勝名稱,所在城市,票價(jià)namecityticket; ; class
3、 cscenenode / 結(jié)點(diǎn)類(lèi) private: head length a1 a2 a3 cscene scene; /內(nèi)嵌對(duì)象cscenenode *prior; /指向前驅(qū)cscenenode *next; /指向后繼friend class cscenelist; /鏈表友元類(lèi)friend class csceneview; /視圖友元類(lèi)public: cscenenode():next(null),prior(null),scene() /構(gòu)造函數(shù)cscenenode(cscene s):next(null),prior(null),scene(s) /構(gòu)造函數(shù); class c
4、scenelist /鏈表類(lèi) private: string listname; /鏈表名int length; / 表長(zhǎng)cscenenode *head; /頭指針friend class csceneview; /視圖友元類(lèi)public: cscenelist():listname(未分類(lèi) ),length(0) /構(gòu)造函數(shù) head=new cscenenode; head-prior=head; head-next=head; cscenelist() /析構(gòu)函數(shù) 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 全部景點(diǎn) endl; cout-next,j=1;p!=head;p=p-next,j+) tscene.citytscene.ticketendl; cout-endl; cout 總數(shù)lengthendl; bool inserthead(cscene scene) /表頭插入,可用任意位置插入函數(shù)代替其功能 cscenenode *pnew=new cscenenode(scene); /申請(qǐng)新結(jié)點(diǎn)if(!pn
6、ew) cout 內(nèi)存申請(qǐng)失敗,插入失??!prior=head; /修改新結(jié)點(diǎn)pnew-next=head-next; /修改新結(jié)點(diǎn)head-next-prior=pnew; /修改鄰近結(jié)點(diǎn)head-next=pnew; /修改頭結(jié)點(diǎn)length+; /表長(zhǎng)加一coutinserted .endl; return true; bool inserttail(cscene scene) /表尾插入 cscenenode *pnew=new cscenenode(scene); /申請(qǐng)新結(jié)點(diǎn)if(!pnew) cout 內(nèi)存申請(qǐng)失敗,插入失?。ext=head; /修改新結(jié)點(diǎn)pnew-prior
7、=head-prior; /修改新結(jié)點(diǎn)head-prior-next=pnew; /修改原末結(jié)點(diǎn)head-prior=pnew; /修改頭結(jié)點(diǎn)length+; /表長(zhǎng)加一return true; bool insert(cscene scene,int i) / 第 i 位置插入 if(i(length+1) /i越界 a pnew head length a1 a2 a3 第 i位置插入函數(shù)圖解cout 指定位置非法! endl; return false; cscenenode *pnew=new cscenenode(scene); /申請(qǐng)新結(jié)點(diǎn)if(!pnew) cout 內(nèi)存申請(qǐng)失敗
8、,插入失??!next,j=1;jnext,j+); /尋找第 i 個(gè)結(jié)點(diǎn)pnew-prior=p-prior; /修改新結(jié)點(diǎn)pnew-next=p; /修改新結(jié)點(diǎn)p-prior-next=pnew; /修改前鄰近結(jié)點(diǎn)p-prior=pnew; /修改后鄰近結(jié)點(diǎn)length+; /表長(zhǎng)加一coutinserted .endl; return true; bool remove(int i) /刪除第 i 個(gè)元素 if(ilength) /i 越界 cout 指定位置非法 ,刪除失?。?next,j=1;jnext,j+); /尋找第 i 個(gè)結(jié)點(diǎn)p-prior-next=p-next; /修改被刪
9、元素前鄰近結(jié)點(diǎn)p-next-prior=p-prior; /修改被刪元素后鄰近結(jié)點(diǎn)delete p; /刪除元素 coutremoved.next;p!=head;p=q) q=p-next; delete p; p-next=head; p-prior=head; listname= 未分類(lèi) ; 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 個(gè)元素 p return *p; bool merge(cscenelist &lista,cscenelist &listb) /有序歸并,按票價(jià)升序排列 clear(); /清空原有元素cscenenode *pa=lista.head-next; /指向 lista 待插元素cscenenode *pb=listb.head-next; /指向 listb 待插元素cscenenode *pc=
11、head; /指向待插位置前驅(qū)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); /修改頭結(jié)點(diǎn)h
12、ead-prior-next=head; / 修改尾結(jié)點(diǎn)length=lista.length+listb.length; / 計(jì)算新表表長(zhǎng)lista.head -prior=lista.head; / 將 a 表置空l(shuí)ista.head-next=lista.head; lista.length =0; listb.head -prior=listb.head; / 將 b 表置空l(shuí)istb.head-next=listb.head; listb.length =0; return true; bool read(string filename) /從文件讀入數(shù)據(jù) ifstream file
13、(filename.c_str(); if(!file) pa 有序歸并函數(shù)圖解pb head length a1 a2 a3 listb head length a1 a2 a3 lista pc a head length cout 文件打開(kāi)失??! listname; /讀取鏈表名filelength; /讀取表長(zhǎng)信息for(int i=1;scene.cityscene.ticket; inserttail(scene); return true; bool write(string filename) /輸出數(shù)據(jù)到文件 ofstream file(filename
14、.c_str(); if(!file) cout 文件保存失敗! endl; return false; filelistnameendl; /保存鏈表名filelengthnext;p!=head;p=p-next) /保存景點(diǎn)信息 tscene.citytscene.ticketendl; file.close(); return true; ; class csceneview / 視圖類(lèi) private: bool modified; /數(shù)據(jù)是否發(fā)生修改的狀態(tài)標(biāo)志string filename; /文件名cscenelist list1; /內(nèi)嵌鏈表對(duì)象 p
15、ublic: csceneview() : modified(false),filename(),list1() /構(gòu)造函數(shù)bool savemodified() /判斷、提示是否需要保存數(shù)據(jù) if(!modified) return false; while(1) cout c; switch(c) case y: case y: return true; case n: case n: return false; void save() /保存數(shù)據(jù)到指定文件 if(!filename.length() /判斷文件名是否為空 cout filename; else if(!modified)
16、 return; list1.write(filename); modified = false; void open() /指定文件名,讀取數(shù)據(jù) cout filename; if(!list1.read(filename) cout !打開(kāi)文件失敗 ! endl; modified = false; void commandcycle() /菜單函數(shù) ,顯示菜單,輸入并解析指令 int command=1; /指令int i; / 用于表示操作元素位序和子指令string name; /名勝名或文件名,用于查找和加載cscene scene1; /景點(diǎn)類(lèi)對(duì)象cscenelist lista
17、,listb; /鏈表類(lèi)對(duì)象,用于歸并暫存cout*endl; cout 名勝管信息管理系統(tǒng)endl; cout*endl; while(command!=0) cout 請(qǐng)選擇操作: 1.插入 2.查詢 3.刪除 4.輸出所有 5.有序歸并 6.保存 7.打開(kāi) 0.退出 command; switch(command) case 1: scene1.input(); couti; if(list1.insert(scene1,i) modified = true; break; case 2: coutname; cout查找結(jié)果如下 endl; list1.find(name); break; case 3: couti; if(list1.remove(i) modified = true; break; case 4: list1.output(); break; case 5: cout 請(qǐng)依次輸入要?dú)w并的有序表的文件名:a、bendl; coutname; lista.read(name); coutname; listb.read(name); if(savemodified() save(); list1.merge(l
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年華師大新版七年級(jí)生物下冊(cè)月考試卷含答案
- 2025年湘教新版九年級(jí)歷史下冊(cè)階段測(cè)試試卷含答案
- 2025年浙教版必修1歷史下冊(cè)月考試卷
- 2025年人教A新版七年級(jí)科學(xué)下冊(cè)階段測(cè)試試卷含答案
- 2025年蘇教新版九年級(jí)歷史下冊(cè)月考試卷
- 2025年仁愛(ài)科普版選擇性必修1語(yǔ)文上冊(cè)階段測(cè)試試卷含答案
- 二零二五版木材加工廢棄物處理合同3篇
- 二零二五年度苗圃場(chǎng)租賃與環(huán)保技術(shù)應(yīng)用合同3篇
- 承包協(xié)議合同(2篇)
- 二零二五版農(nóng)業(yè)用地流轉(zhuǎn)合同范本(含政府補(bǔ)貼條款)3篇
- 【語(yǔ)文】第23課《“蛟龍”探?!氛n件 2024-2025學(xué)年統(tǒng)編版語(yǔ)文七年級(jí)下冊(cè)
- 加強(qiáng)教師隊(duì)伍建設(shè)教師領(lǐng)域?qū)W習(xí)二十屆三中全會(huì)精神專(zhuān)題課
- 2024-2025學(xué)年人教版數(shù)學(xué)七年級(jí)上冊(cè)期末復(fù)習(xí)卷(含答案)
- 2024年決戰(zhàn)行測(cè)5000題言語(yǔ)理解與表達(dá)(培優(yōu)b卷)
- 四年級(jí)數(shù)學(xué)上冊(cè)人教版24秋《小學(xué)學(xué)霸單元期末標(biāo)準(zhǔn)卷》考前專(zhuān)項(xiàng)沖刺訓(xùn)練
- 2025年慢性阻塞性肺疾病全球創(chuàng)議GOLD指南修訂解讀課件
- (完整版)減數(shù)分裂課件
- 銀行辦公大樓物業(yè)服務(wù)投標(biāo)方案投標(biāo)文件(技術(shù)方案)
- 第01講 直線的方程(九大題型)(練習(xí))
- 微粒貸逾期還款協(xié)議書(shū)范本
- 人教版七年級(jí)上冊(cè)數(shù)學(xué)全冊(cè)課時(shí)練習(xí)帶答案
評(píng)論
0/150
提交評(píng)論