




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 壓力容器用鋼材焊接工藝要求考核試卷
- 汽車空調系統風道流動與壓力損失分析考核試卷
- 領導力在遠程團隊管理中的應用考核試卷
- 機器制造企業寧靜生產尺度化評定尺度
- 公司代理合同13篇
- 正規的離婚協議書(資料15篇)
- 污水安全治理活動方案
- 模特在田園走秀活動方案
- 汽車趣味游戲活動方案
- 民俗活動儀仗隊活動方案
- GB/T 30571-2014金屬冷沖壓件通用技術條件
- GB/T 13531.3-1995化妝品通用檢驗方法濁度的測定
- 地基處理教材課件
- DB22-T 2589-2016 聚丙烯腈基碳纖維布
- 基于R語言數據挖掘課程期末論文
- 出國留學申請表-學生模板
- 渣土運輸安全責任書
- 注塑模具課程設計說明書罩蓋
- 2022年杭州市中小學教師職稱考試卷
- 廣東佛山生育保險待遇申請表
- 機械原理課程設計說明書精壓機
評論
0/150
提交評論