




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗題 2.2 編寫一個程序 algo2-2.cpp ,實現(xiàn)單鏈表的各種基本運算,并在此基礎上設計 個主程序完成如下功能。(1)初始化單鏈表 h;(2)依次采用尾插法插入 a,b,c,d,e 元素;(3)輸出單鏈表 h;(4)輸出單鏈表 h 長度;(5)判斷單鏈表 h 是否為空;(6)輸出單鏈表 h 的第 3 個元素;(7)輸出元素 a 的位置;(8)在第4個兀素位置上插入f兀素(9)輸出單鏈表 h;(10)刪除 h 的第 3 個兀素;(11)輸出單鏈表 h;(12)釋放單鏈表 h;程序:#include#include#define TRUE 1#define FALSE 0#define
2、OK 1#define ERROR 0#define NULL 0typedef int Status;typedef int ElemType;typedef struct LNode ElemType data;struct LNode *next;LNode,*LinkList;Status InitList_h(LinkList &h) /初始化線性表h=(LinkList )malloc(sizeof(LNode);/h 指向頭節(jié)點,頭節(jié)點數(shù)據(jù)域為空h-next=NULL;return OK;/ InitList_hStatus DispList_h(LinkList &h) /輸出
3、線性表LinkList p=h-next; while(p!=NULL)printf(%c,p-data);p=p-next; return OK; / DispList_hStatus CreateList_h(LinkList &h,ElemType a,int n) LinkList s,r;int i;h=(LinkList )malloc(sizeof(LNode); r=h;for(i=0;idata=ai;r-next=s;/尾插法建表r=s; r-next=NULL; return OK;/ CreateList_hStatus ListLength_h(LinkList h)
4、 LinkList p=h;int n=0; while(p-next!=NULL) /求線性表的長度n+; p=p-next; return(n);/ ListLength_hStatus ListEmpty_h(LinkList h) return(h-next=NULL);/ ListEmpty_h/判斷單鏈表是否為空Status DestroyList_h(LinkList &h) LinkList p=h,q=p-next;while(q!=NULL)free(p);p=q;q=p-next;free(p);/銷毀線性表return OK;/ DestroyList_hStatus
5、GetElem_h(LinkList h, int i, ElemType &e) / h 為帶頭節(jié)點的單鏈表的頭指針。/ 當?shù)?i 個元素存在時,其值賦給 e 并返回 OK ,否則返回 ERRORint j=0;LinkList p=h;while(jnext;if(p=NULL)return ERROR;elsee=p-data;return OK;/ GetElem_hStatus ListInsert_h(LinkList &h, int i, ElemType e) /插入數(shù)據(jù)元素int j=0;LinkList p=h,s;/*i=2 時,表找到插入節(jié)點的上一個元素,如果是頭節(jié)點則
6、退出,當 i=1 時表示頭節(jié)點, 示第一個元素*/while(jnext;if(p=NULL)return ERROR;elses=(LinkList )malloc(sizeof(LNode); s-data=e;s-next=p-next;p-next=s;return OK;/ ListInsret_hStatus ListDelete_h(LinkList &h, int i, ElemType &e) / 刪除數(shù)據(jù)元素int j=0;LinkList p=h,q; while(jnext;if(p=NULL)return ERROR;else/查找刪除元素的前一個節(jié)點q=p-next
7、; if(q=NULL) return ERROR; e=q-data; p-next=q-next; free(q); return OK;/q 為要刪除的元素節(jié)點/e 為刪除節(jié)點的數(shù)據(jù)區(qū)域/ ListDelete_hint LocateElem_h(LinkList h, ElemType e) LinkList p=h-next;int i=1;while(p!=NULL&p-data!=e)p=p-next;i+;/如果要插入的節(jié)點為頭節(jié)點,則退出 if(p=NULL)return ERROR;else/按元素值查找元素return(i);/ LocateElem_h int main
8、() ElemType e,a5=a,b,c,d,e;LinkList h;printf(1) 初始化單鏈表 hn);InitList_h(h);/ 初始化單鏈表 hprintf(2)依次采用尾插法插入 a,b,c,d,e元素n);CreateList_h(h,&a0,5);/依次采用尾插入法插入printf(3) 輸出單鏈表 h: );DispList_h(h);printf(n);printf(4) 單鏈表 h 的長度為: ); printf(%d,ListLength_h(h);printf(n);if(ListEmpty_h(h)printf(5) 該單鏈表為空 n);elseprin
9、tf(5) 該單鏈表不為空 n);GetElem_h(h,3,e);printf(6) 單鏈表 h 的第三個元素為: printf(%c,e); printf(n);printf(7) 單鏈表 h 中 a 的位置為: printf(%d,LocateElem_h(h,a); printf(n);ListInsert_h(h,4,f);printf(8) 在第 4 個元素位置上插入a,b,c,d,e 元素/ 輸出單鏈表 h/輸出單鏈表 h 的長度/ 判斷單鏈表 h 是否為空);輸出單鏈表h的第3個元素);/輸出元素a的位置/在第4個元素位置插入f元素 f元素n);/刪除 h 的第 3 個元素/輸
10、出單鏈表 h/釋放單鏈表hprintf(9) 輸出單鏈表 h:);DispList_h(h);/輸出單鏈表 hprintf(n);ListDelete_h(h,3,e);printf(10) 刪除 h 的第 3 個元素 n); printf(11) 輸出單鏈表 h:); DispList_h(h);printf(n);printf(12) 釋放單鏈表 hn);DestroyList_h(h);return 0;個主程序完成實驗 2.3 編編寫一個程序 , 實現(xiàn)雙鏈表的各種基本運算 , 并在此基礎上設計 如下功能 .(1) 初始化雙鏈表 h;(2) 依次采用尾插法插入 a,b,c,d,e 元素;
11、(3) 輸出雙鏈表 h;(4) 輸出雙鏈表 h 的長度;(5) 判斷雙鏈表 h 是否為空;(6) 輸出雙鏈表 h 的第 3 個元素;(7) 輸出元素 a 的位置;(8) 在第 4 個元素位置上插入 f 元素;(9) 輸出雙鏈表 h;(10) 刪除h的第3個元素;(11) 輸出雙鏈表 h;(12) 釋放雙鏈表 h;#include #includetypedef char ElemType;typedef struct DNodeElemType data;struct DNode *prior;struct DNode *next;DLinkList;void InitList(DLinkLi
12、st *&h)h=(DLinkList *)malloc(sizeof(DLinkList);h-prior=h-next=NULL;void DestroyList(DLinkList *&h)DLinkList *p=h,*q=p-next;while(q!=NULL)free(p);p=q;q=q-next;free(p);int ListEmpty(DLinkList *h) return(h-next=NULL);int ListLength(DLinkList *h)DLinkList *p=h;int i=0;while(p-next!=NULL)i+;p=p-next;retu
13、rn(i);void DispList(DLinkList *h) DLinkList *p=h-next;while(p!=NULL)printf(%c ,p-data);p=p-next;printf(n);int GetElem(DLinkList *h,int i,ElemType &e) int j=0;DLinkList *p=h;while(jnext;if(p=NULL)return 0;else e=p-data;return 1;int LocateElem(DLinkList *h,ElemType e) int n=1;DLinkList *p=h-next; whil
14、e(p!=NULL &p-data!=e) n+;p=p-next; if(p=NULL) return 0;elsereturn n;int ListInsert(DLinkList *&h,int i,ElemType e) int j=0;DLinkList *p=h,*s;while(jnext;if(p=NULL)return 0;else s=(DLinkList *)malloc(sizeof(DLinkList); s-data=e;s-next=p-next; if(p-next!=NULL)p-next-prior=s; s-prior=p;p-next=s;return
15、1;int ListDelete(DLinkList *&h,int i,ElemType &e)int j=0;DLinkList *p=h,*q;while(jnext;if(p=NULL)return 0;elseq=p-next;if(q=NULL)return 0;p-next=q-next;if(p-next!=NULL)p-next-prior=p;free(q);return 1;/實現(xiàn)雙鏈表各種基本運算的算法 .cpp void main()DLinkList *h;ElemType e; printf(1) 初始化雙鏈表 h:n);InitList(h);printf(2) 依次采用尾插法插入 a,b,c,d,e 元素 n);ListInsert(h,1,a);ListInsert(h,2,b);ListInsert(h,3,c);ListInsert(h,4,d);ListInsert(h,5,e); printf(3) 輸出雙鏈表 h:);DispList(h);printf(4) 雙鏈表 h 的長度 =%dn,ListLength(h);printf(5)雙鏈表 h 為sn,(ListEmpty(h)?空:非空);GetElem(h,3,e);printf(6) 雙鏈表
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學英語名詞單復數(shù)專項測試
- 法學入學面試題及答案
- 民航消防面試題及答案
- 2024年廣告設計師考試獨特視角試題及答案
- 出國勞務面試題目及答案
- 餐館收銀面試題目及答案
- 2024國際美術設計師考試整合知識點試題及答案
- 2024教育學試題及答案
- 2024年紡織品設計師證書考試與行業(yè)標準試題及答案
- 創(chuàng)意思維在廣告中的應用試題及答案
- 《大學心理》筆記(1-14章節(jié))
- 《日語聽說》課件-第六課 餐館就餐
- 舞蹈房入股合同模板
- 言語治療技術說評估CRRCAE法
- 醫(yī)療廢物與醫(yī)療污水處理
- 中華人民共和國能源法
- 鋼結構隔層施工合同范本
- 季度工作總結報告模板
- 跟骨骨折護理查房課件
- 《資本論》(德)卡爾·馬克思-文字版
- 多模態(tài)交互反饋機制
評論
0/150
提交評論