#include<stdio.h>#include<stdlib.h>typedefstructnode//结构体{int p;//系数int power;//指数structnode*next;}LNode,*Linklist;voidPrintList(Linklist L)//打印链表{int a;
L = L -> next;if(L -> power ==0){printf("%d ",L -> p);
L = L -> next;}else{if(L -> p ==1&& L -> power !=1){printf("X^%d ",L -> power);
L = L -> next;}elseif(L -> power ==1&& L -> p !=1){printf("%d * X ",L -> p,L -> power);
L = L -> next;}elseif(L -> p ==1&& L -> power ==1){printf("X ",L -> p,L -> power);
L = L -> next;}else{printf("%d * X^%d ",L -> p,L -> power);
L = L -> next;}}while(L !=NULL){if(L -> power !=0&& L -> p >0){printf("+ %d * X^%d ",L -> p,L -> power);
L = L -> next;}elseif(L -> p <0){
a =0- L -> p;printf("- %d * X^%d ",a,L -> power);
L = L -> next;}}printf("\n");}
Linklist Listcreat()//建立链表{int p,power;
Linklist L;
LNode *r,*s;
L =(Linklist)malloc(sizeof(LNode));
r = L;printf("输入指数和系数:");scanf("%d,%d",&p,&power);//输入系数和指数while(p !=0){
s =(Linklist)malloc(sizeof(LNode));
s -> p = p;
s -> power = power;
r -> next = s;
r = s;printf("输入指数和系数,当系数为零时结束输入:");scanf("%d,%d",&p,&power);}
r -> next =NULL;return L;}
Linklist Listadd(Linklist La,Linklist Lb)//多项式相加{
Linklist a = La -> next;
Linklist b = Lb -> next;
Linklist H,pc,qc;
pc =(Linklist)malloc(sizeof(LNode));
pc -> next =NULL;
H = pc;while(a !=NULL&& b !=NULL)//两个链表未扫描完时{
qc =(Linklist)malloc(sizeof(LNode));if(a -> power < b -> power)//la中指数小于lb中指数{
qc -> p = a -> p;
qc -> power = a -> power;
a = a -> next;}elseif(a -> power == b -> power){
qc -> p = a -> p + b -> p;
qc -> power = a -> power;
a = a -> next;
b = b -> next;}else{
qc -> p = b -> p;
qc -> power = b -> power;
b = b -> next;}if(qc -> p !=0){
qc -> next = pc -> next;
pc -> next = qc;
pc = qc;}elsefree(qc);}while(a !=NULL){
qc =(Linklist)malloc(sizeof(LNode));
qc -> p = a -> p;
qc -> power = a -> power;
a = a -> next;
qc -> next = pc -> next;
pc -> next = qc;
pc = qc;}while(b !=NULL){
qc =(Linklist)malloc(sizeof(LNode));
qc -> p = b -> p;
qc -> power = b -> power;
b = b -> next;
qc -> next = pc -> next;
pc -> next = qc;
pc = qc;}return H;}
Linklist Listsubtraction(Linklist La,Linklist Lb)//多项式相减{
Linklist H = Lb;
Linklist b = Lb -> next;while(b -> next !=NULL){
b -> p =(-1)* b -> p;
b = b -> next;}
Linklist Q =Listadd(La,H);return Q;}
Linklist Listderivatives(Linklist L)//多项式求导{
Linklist a = L -> next;
Linklist H;
LNode *r,*s;
r =(Linklist)malloc(sizeof(LNode));
r -> next =NULL;
H = r;while(a !=NULL){
s =(Linklist)malloc(sizeof(LNode));if(a -> power ==0){
a = a -> next;}else{
s -> p = a -> p * a -> power;
s -> power = a -> power -1;
r -> next = s;
r = s;
a = a -> next;}
r -> next =NULL;}return H;}voidmain(){
Linklist la,lb,P,Q,R,S;printf("输入多项式la数据\n");
la =Listcreat();printf("输入多项式lb数据\n");
lb =Listcreat();printf("la = ");PrintList(la);//打印laprintf("lb = ");PrintList(lb);//打印lbprintf("la + lb = ");
P =Listadd(la,lb);//la和lb相加PrintList(P);printf("la - lb = ");
Q =Listsubtraction(la,lb);//la和lb相减PrintList(Q);printf("la' = ");
R =Listderivatives(la);//la求导PrintList(R);printf("lb' = ");
S =Listderivatives(lb);//lb求导PrintList(S);}