利用链表处理多项式.ppt
《利用链表处理多项式.ppt》由会员分享,可在线阅读,更多相关《利用链表处理多项式.ppt(33页珍藏版)》请在三一文库上搜索。
1、利用链表处理多项式,程序分块,1.编出制造多项式的函数creatlist(); llink creatlist()/制造多项式 llink head,p; int code,j,k,guard; printf(“请输入多项式项数n“); label: k=scanf(“%d“, ,printf(“按系数-指数的顺序初始化多项式n“); printf(“输入格式:系数和指数间有一逗号隔开n“); head=(llink)malloc(sizeof(node); if(!head) printf(“内存分配失败n“); exit(1); head-fin=-1; head-tak=-1; head
2、-next=NULL; p=head;,for(j=0;jnext=(llink)malloc(sizeof(node); if(!p-next) printf(“内存分配失败n“); exit(1); guard=scanf(“%f,%f“, ,p-next-next=NULL; p=p-next; p-next=head;/创建循环链表 return head; 编出制造多项式函数后,下面编造其他功能函数 /计算多项式值的函数,要把头指针传进去 double result(llink head) llink p; float x; double sum=0; p=head-next; pr
3、intf(“请输入未知数的值n“); scanf(“%f“,while(p!=head) sum+=(p-tak)*pow(x,p-fin); p=p-next; return sum; /多项式求导函数,把多项式的头指针传进去 qiudao (llink head) llink p,q,r;,p=head-next; r=(llink)malloc(sizeof(node);/设置头结点,为创建循环链表 if(!r) printf(“内存分配失败n“); exit(1); r-fin=-1; r-tak=-1; q=r; q-next=NULL;,while(p!=head) q-next=
4、(llink)malloc(sizeof(node); if(!q-next) printf(“内存分配失败n“); exit(1); q-next-next=NULL; q-next-fin=p-fin-1; q-next-tak=(p-fin)*(p-tak); p=p-next; q=q-next; q-next=r;/将链表首尾连接起来,创建循环链表,printf(“求导后的多项式是:“); q=r-next; while(q!=r) printf(“%.2fX%.2f“,q-tak,q-fin); q=q-next; if(q-tak0 ,/多项式求积分函数,要传入多项式头指针传进去
5、 jifen(llink head) llink p,q,r; p=head-next; r=(llink)malloc(sizeof(node); if(!r) printf(“内存分配失败n“); exit(1); r-next=NULL; r-fin=-1; r-tak=-1; q=r,while(p!=head) q-next=(llink)malloc(sizeof(node); if(!q-next) printf(“内存分配失败n“); exit(1); q-next-fin=p-fin+1;/根据积分运算原则,来为新多项式初始化 q-next-tak=p-tak/q-next-
6、fin; p=p-next; q=q-next; q-next=r; q=r; q=q-next;,while(q!=r) printf(“%.2fX%.2f“,q-tak,q-fin); q=q-next; if(q-tak0 ,/计算两多项式和的函数的函数 llink add(llink plo,llink plp) llink head1,head2,result,before,check,another; head1=plo-next; head2=plp-next; result=(llink)malloc(sizeof(node);/创建头结点,为创建循环链表 if(!result
7、) return NULL; result-fin=-1;/头结点初始化 result-tak=-1; before=result;/另一同类型指针来造出剩余结点,制造头结点的指针不能改变,以便返回该指针 before-next=NULL;,while(plo!=head1|plp!=head2)/分三种情况,依据指数的大小分类 before-next=(llink)malloc(sizeof(node); if(!before-next) printf(“内存分配失败n“); exit(1); before-next-next=NULL; if(head1-finhead2-fin) bef
8、ore-next-fin=head1-fin; before-next-tak=head1-tak; head1=head1-next; before=before-next; ,else if(head1-finfin) before-next-fin=head2-fin; before-next-tak=head2-tak; head2=head2-next; before=before-next; else before-next-fin=head1-fin; before-next-tak=head1-tak+head2-tak; head1=head1-next; head2=hea
9、d2-next; before=before-next; ,before-next=result;/将链表首尾连接起来,创建循环链表 check=result-next; another=result-next; while(another-fin!=-1) if(another-fin=check-next-fin) another-tak+=check-next-tak; check-next=check-next-next; check=check-next; if(check-fin=-1) another=another-next; check=another; return res
10、ult; ,/多项式相减 llink minus(llink plo,llink plp) llink head1,head2,result,before,check,another; head1=plo-next; head2=plp-next; result=(llink)malloc(sizeof(node);/创建头结点,为创建循环链表 if(!result) return NULL; result-fin=-1;/头结点初始化 result-tak=-1; before=result;/另一同类型指针来造出剩余结点,制造头结点的指针不能改变,以便返回该指针 before-next=N
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 利用 处理 多项式
链接地址:https://www.31doc.com/p-2881688.html