




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
數(shù)據(jù)結構實驗參考代碼實驗一:針對鏈式或順序存儲的線性表實現(xiàn)指定的操作題1 問題描述:有兩個指數(shù)遞減的一元多項式,寫一程序先求這兩個多項式的和,再求它們的積。基本要求:用帶表頭結點的單鏈表作為多項式的存儲表示;要建立兩個單鏈表;多項式相加就是要把一個單鏈表中的結點插入到另一個單鏈表中去,要注意插入、刪除操作中指針的正確修改。題2 問題描述:編號為1,2,n的n個人圍坐在一圓桌旁,每人持有一個正整數(shù)的密碼。從第一個人開始報數(shù),報到一個預先約定的正整數(shù)m時,停止報數(shù),報m的人退席,下一個人又重新從1開始報數(shù),依此重復,直至所有的人都退席。編一程序輸出他們退席的編號序列。例如,設m=20,n=7,7個人的密碼依次是3,1,7,2,4,8,4,則退席的人的編號依次為6,1,4,7,2,3,5。基本要求:用不帶表頭結點的循環(huán)單鏈表表示圍成圓圈的n個人;要求建立此循環(huán)單鏈表;某人離席相當于刪除一個結點,要正確設置程序中循環(huá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;/實驗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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藥劑研究新方法探索試題及答案
- 調整思維方式2024年系統(tǒng)規(guī)劃與管理師考試試題及答案
- 文化產業(yè)管理證書考試中的時間管理與試題及答案
- 激光技術的創(chuàng)新應用案例試題及答案
- 財務分析與稅務籌劃結合試題及答案
- 藥劑學臨床應用的前沿問題試題及答案
- 解析公共營養(yǎng)師考試必考知識試題及答案
- 適合零基礎的計算機二級試題及答案
- 計算機二級考試實踐案例試題及答案
- 探索文化產業(yè)管理考試中的信息技術應用與管理效率提升試題及答案
- GB/T 11834-2011工農業(yè)機械用摩擦片
- 全文《中國式現(xiàn)代化》PPT
- 藥品零售企業(yè)許可事項申請表模板
- 經尿道前列腺剜除術講解
- 物業(yè)公司xx年度收支情況公示模板
- 必修二英語單詞默寫
- 新人教版四年級數(shù)學下冊總復習專題一《四則運算及運算定律》課件
- 宋詞欣賞《虞美人·聽雨》課件
- 混合痔病歷范文
- 110kV線路光纜施工方案及安全管控
- 35KV高壓開關柜買賣合同
評論
0/150
提交評論