#ifndef POLY_H_INCLUDED
#define POLY_H_INCLUDED
//with header
struct polynode
{
int C;
int n;
struct polynode* next;
};
typedef struct polynode* list;
list init_list(list L)
{
L=(list)malloc(sizeof(struct polynode));
L->next = NULL;
return L;
}
void print_list(list L)
{
list p=L;
while (p->next!=NULL)
{
printf("%dX^%d",p->next->C,p->next->n);
if(p->next->next!=NULL) putchar('+');
p=p->next;
}
printf("\n");
}
list find(list head,int n) //在list中返回n的前导节点指针
{
while (head->next!=NULL)
{
if (head->next->n==n) break;
head = head->next;
}
return head;
}
void insert_list(list L,int C,int power) //插入
{
list r = find(L,power);
if (r->next!=NULL) r->next->C+=C;
else
{
list tmp = (list)malloc(sizeof(struct polynode));
tmp->C = C;
tmp->n = power;
list p=L;
while (p->next!=NULL)
{
if (p->next->n < power) break;
p=p->next;
}
tmp->next = p->next;
p->next = tmp;
}
}
void poly_mult(list A,list B,list result)
{
int c,n;
list p,q;
p=A;
int tmp_n,tmp_c;
while (p->next!=NULL)
{
n=p->next->n;
c=p->next->C;
q=B;
while (q->next!=NULL)
{
tmp_n=q->next->n + n;
tmp_c=q->next->C*c;
insert_list(result,tmp_c,tmp_n);
q=q->next;
}
p=p->next;
}
}
#endif // POLY_H_INCLUDED