文档详情

数据结构实验代码案例.doc

发布:2017-06-18约4.86千字共13页下载文档
文本预览下载声明
《数据结构》实验代码 实验一:针对链式或顺序存储的线性表实现指定的操作 题1 问题描述:有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。 题2 问题描述:编号为1,2,···,n的n个人围坐在一圆桌旁,每人持有一个正整数的密码。从第一个人开始报数,报到一个预先约定的正整数m时,停止报数,报m的人退席,下一个人又重新从1开始报数,依此重复,直至所有的人都退席。编一程序输出他们退席的编号序列。例如,设m=20,n=7,7个人的密码依次是3,1,7,2,4,8,4,则退席的人的编号依次为6,1,4,7,2,3,5。 n个人;要求建立此循环单链表;某人离席相当于删除一个结点,要正确设置程序中循环终止的条件和删除结点时指针的修改变化。 #includeiostream using namespace std; struct poNode { float coef; int expn; poNode *next; }; class Polynomail { public: 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项数endl; cinm; Polynomail P1(m); if(P1.PolynLength()!=m) { couterror!endl; return -1; } cout输入多项式P2项数endl; cinm; 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输入系数和指数:endl; cinfirst-coef; cinfirst-expn; first-next=NULL; } else { poNode *p=new poNode; poNode *q=first; cout输入系数和指数:endl; cinp-coef; cinp-expn; poNode *r; while(q-next!=NULLq-expnp-expn) { r=q; q=q-next; } if(q==firstq-next!=NULL) { if(q-expn==p-expn) { return -1; } p-next=q; first=p; } else if(q==firstq-next==NULL) { if(p-expnq-expn) { p-next=q; first=p; } else if(p-expnq-expn) { q-next=p; p-next=NULL; } else { return -1; } } else if(q-next==NULLq!=firstp-expnq-expn) { q-next=p; p-next=NULL; } else { if(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;
显示全部
相似文档