7-6 多项式的加法 (50 分)
用链表表示多项式,并实现多项式的加法运算
输入格式:
输入在第一行给出第一个多项式POLYA的系数和指数,并以0,0 结束第一个多项式的输入;在第二行出第一个多项式POLYB的系数和指数,并以0,0 结束第一个多项式的输入。
输出格式:
对每一组输入,在一行中输出POLYA+POLYB和多项式的系数和指数。
第二个测试点一直过不去
```c
#include<stdio.h>
#include<stdlib.h>
#define maxSize 100
struct List{
int data_1;
int data_2;
struct List *next;
};
struct List *creat(){
struct List *head=NULL;
struct List *new;
struct List *end;
new=(struct List*)malloc(maxSize*sizeof(struct List*));
head=new;
for(int i=0;;i++){
scanf("%d",&new->data_1);
getchar();
scanf("%d",&new->data_2);
end=new;
if((new->data_1==0)&&(new->data_2==0)){
new->next=NULL;
return head;
}
new = (struct List*)malloc(maxSize*sizeof(struct List*));
end->next = new;
}
}
void plb(struct List *head)
{
struct List *temp;
temp = head;
while (temp->next!=NULL)
{
if(temp->data_1==0){
temp=temp->next;
continue;
}
printf("%d,%d", temp->data_1,temp->data_2);
if (temp->next!=NULL)
printf(" ");
temp = temp->next;
}
}
int main(){
struct List *head_1,*head_2;
head_1 = creat();
head_2 = creat();
struct List *temp_1,*temp_2;
temp_1 = head_1;
temp_2 = head_2;
struct List *Head=NULL;
struct List *New;
struct List *End;
New=(struct List*)malloc(maxSize*sizeof(struct List*));
Head=New;
int count = 0;
while(temp_2->next!=NULL&&temp_1->next!=NULL){
if(temp_1->data_2<temp_2->data_2){
New->data_1 = temp_1->data_1;
New->data_2 = temp_1->data_2;
End = New;
New = (struct List *) malloc(maxSize * sizeof(struct List *));
End->next = New;
temp_1=temp_1->next;
continue;
}
else if(temp_1->data_2==temp_2->data_2) {
New->data_1 = temp_1->data_1 + temp_2->data_1;
New->data_2 = temp_1->data_2;
End = New;
New = (struct List *) malloc(maxSize * sizeof(struct List *));
End->next = New;
temp_1=temp_1->next;
temp_2=temp_2->next;
continue;
}
else if(temp_1->data_2>temp_2->data_2){
New->data_1 = temp_2->data_1;
New->data_2 = temp_2->data_2;
End = New;
New = (struct List *) malloc(maxSize * sizeof(struct List *));
End->next = New;
temp_2=temp_2->next;
continue;
}
}
if(temp_1->next!=NULL&&temp_2->next==NULL){
while(temp_1->next!=NULL){
New->data_1 = temp_1->data_1;
New->data_2 = temp_1->data_2;
End = New;
New = (struct List *) malloc(maxSize * sizeof(struct List *));
End->next = New;
temp_1=temp_1->next;
}
}
else if(temp_1->next==NULL&&temp_2->next!=NULL){
while(temp_2->next!=NULL){
New->data_1 = temp_2->data_1;
New->data_2 = temp_2->data_2;
End = New;
New = (struct List *) malloc(maxSize * sizeof(struct List *));
End->next = New;
temp_2=temp_2->next;
}
}
New->next=NULL;
plb(Head);
return 0;
}
```