# 一元稀疏多项式（加减法）

1.当指数相等时，系数相加（减法时则相减）；相加完的系数若不为0，则存进链表C；

2.若指数不相等，若A的指数小于B的，将A的节点里的系数，指数都存进链表C；

3.反之，则将B的节点里的系数，指数都存进链表C；

4.当有一个链表的后几项多出来时，也将其存进链表C。

1.头结点不能变，在操作时应另外申请一个节点指向头节点或首元节点进行操作。

2.在循环中输入每一个数据前，都要申请节点空间。

3.注意指针指向的地点，注意移动指针和赋值的顺序。

4.注意链表输入的结尾，要有输入结束的判断，并且尾指针为空。

5.指针函数最后要返回一个指针节点，函数的类型也是结构体的类型。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PolyNode  // 结构体
{
float coef;  //系数
int exp;     //指数
struct PolyNode * next;
}PolyNode;
typedef PolyNode * Polynomial;   // 类型声明
Polynomial  CreateList()    //建表并输入
{
Polynomial L,p,r;
r = L = (Polynomial )malloc(sizeof(PolyNode ));   //头节点是L不能移动
L->next = NULL;
float x;
int y;
scanf("%f %d",&x,&y);
while(x && y)   //判断输入结束的条件为( x!=0 && y!=0 )
{
p = (Polynomial )malloc(sizeof(PolyNode ));
p->coef = x;
p->exp = y;
p->next = NULL;
r->next = p;
r = p;
scanf("%f %d",&x,&y);
getchar();  //读掉 0 0 之后的回车
}
return L;  // 返回输入的链表
}
Polynomial Add(Polynomial A, Polynomial B)  //加法
{
Polynomial C,S;
Polynomial pa,pb,pc;
float x;
pa = A->next;
pb = B->next;
C = (Polynomial )malloc(sizeof(PolyNode ));  // 申请头节点
pc = C;   // 操作时使用pc指针
pc->next = NULL;
while (pa && pb)
{
if(pa->exp == pb->exp)  // 指数相等时
{
x = pa->coef + pb->coef;
if (x)     // 相加完的系数不为0时
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = x;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
}
pa = pa->next;
pb = pb->next;
}
else
if(pa->exp < pb->exp)   //指数不相等且A项指数小于B项指数时
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pa->coef;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
pa = pa->next;
}
else   //指数不相等且A项指数大于B项指数时
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pb->coef;
S->exp = pb->exp;
S->next = NULL;
pc->next = S;
pc = S;
pb = pb->next;
}
}
while (pa)   //A链表后几项多出来时
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pa->coef;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
pa = pa->next;
}
while (pb)   //B链表后几项多出来时
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pb->coef;
S->exp = pb->exp;
S->next = NULL;
pc->next = S;
pc = S;
pb = pb->next;
}
return C;
}
Polynomial Minus(Polynomial A, Polynomial B)  //减法，判断条件等都同上
{
Polynomial C,S;
Polynomial pa,pb,pc;
float x;
pa = A->next;
pb = B->next;
C = (Polynomial )malloc(sizeof(PolyNode ));
pc = C;
pc->next = NULL;
while (pa && pb)
{
if(pa->exp == pb->exp)
{
x = pa->coef - pb->coef; //系数相减
if (x)
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = x;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
}
pa = pa->next;
pb = pb->next;
}
else
if(pa->exp < pb->exp)
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pa->coef;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
pa = pa->next;
}
else
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pb->coef;
S->exp = pb->exp;
S->next = NULL;
pc->next = S;
pc = S;
pb = pb->next;
}
}
while (pa)
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pa->coef;
S->exp = pa->exp;
S->next = NULL;
pc->next = S;
pc = S;
pa = pa->next;
}
while (pb)
{
S = (Polynomial )malloc(sizeof(PolyNode ));
S->coef = pb->coef;
S->exp = pb->exp;
S->next = NULL;
pc->next = S;
pc = S;
pb = pb->next;
}
return C;
}
int main()
{
Polynomial A, B, C, pc;
char f;
printf("输入多项式A：\n");
A = CreateList();
printf("输入加减符号：\n");
scanf("%c",&f);
getchar();       //读掉输入完符号后的回车
printf("输入多项式B：\n");
B = CreateList();
C = (Polynomial )malloc(sizeof(PolyNode ));
C->next = NULL;
if(f == '+') C = Add(A , B);
if(f == '-') C = Minus(A , B);
pc = C->next;
while(pc != NULL)
{
printf("链表C为：%.2fX^%d " ,pc->coef, pc->exp);
pc = pc->next;
}
printf("\n");
return 0;
}


03-10

11-18
09-23 358
12-11 3328
02-10 7064
11-02 2108
10-28 1万+
06-28 711
07-23 778