




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)參考代碼實(shí)驗(yàn)一:針對鏈?zhǔn)交蝽樞虼鎯Φ木€性表實(shí)現(xiàn)指定的操作題1 問題描述:有兩個指數(shù)遞減的一元多項式,寫一程序先求這兩個多項式的和,再求它們的積?;疽螅河脦П眍^結(jié)點(diǎn)的單鏈表作為多項式的存儲表示;要建立兩個單鏈表;多項式相加就是要把一個單鏈表中的結(jié)點(diǎn)插入到另一個單鏈表中去,要注意插入、刪除操作中指針的正確修改。題2 問題描述:編號為1,2,n的n個人圍坐在一圓桌旁,每人持有一個正整數(shù)的密碼。從第一個人開始報數(shù),報到一個預(yù)先約定的正整數(shù)m時,停止報數(shù),報m的人退席,下一個人又重新從1開始報數(shù),依此重復(fù),直至所有的人都退席。編一程序輸出他們退席的編號序列。例如,設(shè)m=20,n=7,7個人的密碼依次是3,1,7,2,4,8,4,則退席的人的編號依次為6,1,4,7,2,3,5?;疽螅河貌粠П眍^結(jié)點(diǎn)的循環(huán)單鏈表表示圍成圓圈的n個人;要求建立此循環(huán)單鏈表;某人離席相當(dāng)于刪除一個結(jié)點(diǎn),要正確設(shè)置程序中循環(huán)終止的條件和刪除結(jié)點(diǎn)時指針的修改變化。/實(shí)驗(yàn)1.1代碼#includeusing namespace std;struct poNodefloat coef;int expn;poNode *next;class Polynomailpublic:Polynomail(int m=0); Polynomail();int Print();int PolynLength();Polynomail &AddPolyn(Polynomail &P2,Polynomail &P3);Polynomail &MultiplyPolyn(Polynomail &P2,Polynomail &P4);private:int InsertpoNode();poNode *first;int main()int m;cout輸入多項式P1項數(shù)m;Polynomail P1(m);if(P1.PolynLength()!=m)couterror!endl;return -1;cout輸入多項式P2項數(shù)m;Polynomail P2(m);if(P2.PolynLength()!=m)couterror!endl;return -1;cout多項式P1:;P1.Print();cout多項式P2:;P2.Print();Polynomail P3;P3=P1.AddPolyn(P2,P3);coutP1+P2:;P3.Print();Polynomail P4;P4=P1.MultiplyPolyn(P2,P4);coutP1*P2:;P4.Print();return 0;int Polynomail:InsertpoNode()if(first=NULL)first=new poNode;cout輸入系數(shù)和指數(shù):first-coef;cinfirst-expn;first-next=NULL;elsepoNode *p=new poNode;poNode *q=first;cout輸入系數(shù)和指數(shù):p-coef;cinp-expn;poNode *r;while(q-next!=NULL&q-expnexpn)r=q;q=q-next;if(q=first&q-next!=NULL) if(q-expn=p-expn) return -1;p-next=q;first=p;else if(q=first&q-next=NULL)if(p-expnexpn)p-next=q;first=p;else if(p-expnq-expn)q-next=p;p-next=NULL;else return -1;else if(q-next=NULL&q!=first&p-expnq-expn)q-next=p;p-next=NULL;elseif(q-expn=p-expn)return -1;r-next=p;p-next=q;return 0;Polynomail:Polynomail(int m)first=NULL;int i;for(i=0;im;i+)int r=InsertpoNode();if(r=-1) break;int Polynomail:Print()poNode *p=first;coutf(x)=;if(first=NULL) coutNULLendl;while(p!=NULL)coutcoef*xexpn;if(p-next=NULL) coutendl;else coutnext;return 0;Polynomail:Polynomail()poNode *p=first;while(p!=NULL)p=p-next;delete first;first=p;int Polynomail:PolynLength()int i=0;poNode *p=first;while(p!=NULL)p=p-next;i+;return i;Polynomail &Polynomail:AddPolyn(Polynomail &P2,Polynomail &P3)poNode *p=first;poNode *q=P2.first;P3.first=new poNode;poNode *r=P3.first;while(p!=NULL&q!=NULL)if(p-expn=q-expn) r-coef=p-coef+q-coef;r-expn=p-expn;p=p-next;q=q-next;else if(p-expnexpn)r-coef=p-coef;r-expn=p-expn;p=p-next;elser-coef=q-coef;r-expn=q-expn;q=q-next;if(p!=NULL|q!=NULL)r-next=new poNode;r=r-next;else r-next=NULL;while(p!=NULL|q!=NULL)if(p!=NULL)r-coef=p-coef;r-expn=p-expn;p=p-next;elser-coef=q-coef;r-expn=q-expn;q=q-next;if(p!=NULL|q!=NULL)r-next=new poNode;r=r-next;else r-next=NULL;return P3;Polynomail &Polynomail:MultiplyPolyn(Polynomail &P2,Polynomail &P4)poNode *p=first;while(p!=NULL)Polynomail P3;poNode *q=P2.first;P3.first=new poNode;poNode *r=P3.first;r-coef=p-coef*q-coef;r-expn=p-expn+q-expn;q=q-next;while(q!=NULL)r-next=new poNode;r=r-next;r-coef=p-coef*q-coef;r-expn=p-expn+q-expn;q=q-next;r-next=NULL;P4=P4.AddPolyn(P3,P4);p=p-next;return P4;/實(shí)驗(yàn)1.2代碼#includeusing namespace std;struct numNodeint key;int num;numNode *next;int CreatCircleList(numNode *now,int length);int Loop(numNode *now,int m);int main()int m;int n;numNode *now=new numNode;cout請輸入人數(shù):n;cout請輸入初始數(shù)m:m;CreatCircleList(now,n);Loop(now,m);return 0;int CreatCircleList(numNode *now,int length)if(length=0)couterror!num=1;cout請輸入編號為1的人擁有的密碼。now-key;numNode *p=now;int i;for(i=1;inext=new numNode;p=p-next;p-num=i+1;cout請輸入編號為i+1的人擁有的密碼。p-key;p-next=now;ret
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 殘聯(lián)康復(fù)資金管理辦法
- 團(tuán)隊基金管理辦法課件
- 51單片機(jī)糧倉溫濕度檢測系統(tǒng)設(shè)計
- 大學(xué)生身體活動對體質(zhì)健康影響的中介效應(yīng)研究
- 獸藥原料銷售管理辦法
- 杭州租車公司管理辦法
- 華為海外稅務(wù)管理辦法
- 杭州動物診療管理辦法
- 隔聲結(jié)構(gòu)優(yōu)化-洞察及研究
- 高溫燃燒環(huán)境下木質(zhì)纖維元素分析條件優(yōu)化研究
- 港口裝卸作業(yè)培訓(xùn)
- 2025年湖北省武漢市中考數(shù)學(xué)真題(無答案)
- 鉗工考試試題及答案
- 拖欠維修費(fèi)車輛以車抵債協(xié)議范本
- 2025至2030中國復(fù)印機(jī)行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 暑假安全家長會4
- 2024年安徽省泗縣衛(wèi)生局公開招聘試題帶答案
- 2025年北京市高考化學(xué)試卷真題(含答案)
- 呼倫貝爾農(nóng)墾集團(tuán)有限公司招聘筆試題庫2025
- 醫(yī)院檢驗(yàn)科實(shí)驗(yàn)室生物安全程序文件SOP
- 詢價單(表格模板)
評論
0/150
提交評論