题目描述:通过键盘输入两组多项式的系数和指数,当系数为零时表示输入结束,规定输入的指数是按从小到大顺序的,计算两个一元多项式相加的和多项式并用相同的方式输出
1.建立两个单链表,存储两个一元多项式(尾插法)
typedef struct node{
int c;
int e;
struct node* next;
}Node;
Node* PolyCreate()
{
Node *head,*tail,*s;//head指向头结点,tail指向尾结点,*s是当前结点
head = (node*)malloc(sizeof(node));
tail = head;
int c,e;
cin >> c >> e;
while(c!=0)
{
s = (node*)malloc(sizeof(node));
s->c = c;
s->e = e;
tail->next = s;
tail = s;
cin >> c >> e;
}
tail->next = NULL;
return (head);
}
2.进行多项式相加:分三种情况,具体都写在注释里了;再进行输出
void PolyAdd(Node* pa,Node* pb)
{
Node *p,*q,*tail,*temp;
p = pa->next;
q = pb->next;
tail = pa; //tail指向和多项式链表的尾结点
while(p != NULL && q != NULL)
{
if(p->e == q->e) //系数相等,将二者相