#include<stdio.h>
#include<malloc.h>
typedef int elementtype;
struct node{
elementtype dishu,zhishu;
struct node* next;
};
typedef struct node* List;
List create(List L);
List add(List L1,List L2);
List mult(List L1,List L2);
int main(){
List L1=NULL,L2=NULL;
L1=create(L1);
L2=create(L2);
List p=mult(L1,L2);
int flag=1;
while(p){
if(flag==1){
printf("%d %d",p->dishu ,p->zhishu );
flag=0;
}
else printf(" %d %d",p->dishu ,p->zhishu );
p=p->next ;////
}
flag=1;
printf("\n");
List head=add(L1,L2);
while(head){
if(flag==1){
printf("%d %d",head->dishu ,head->zhishu );
flag=0;
}
else printf(" %d %d",head->dishu ,head->zhishu );
head=head->next ;
}
return 0;
}
List create(List L){
elementtype number;
List p,tail=NULL;
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++){
p=(List)malloc(sizeof(struct node));
p->next =NULL;
scanf("%d %d",&p->dishu,&p->zhishu );
if(L==NULL)L=p;
else{
tail->next =p;
}
tail=p;
}
return L;
}
List add(List L1,List L2){
List p1=L1,p2=L2;
List head=NULL,tail,p;
while(1){
if(p1==NULL||p2==NULL)break;
p=(List)malloc(sizeof(struct node));
p->next =NULL;
if(p1->zhishu ==p2->zhishu ){
p->dishu =p1->dishu +p2->dishu ;
p->zhishu =p1->zhishu ;
p1=p1->next ;
p2=p2->next ;//
}
else if(p1->zhishu >p2->zhishu ){
p->dishu =p1->dishu ;
p->zhishu =p1->zhishu;
p1=p1->next ; //
}
else{
p->dishu =p2->dishu ;
p->zhishu =p2->zhishu;
p2=p2->next ;//
}
if(head==NULL)head=p;
else tail->next =p;
tail=p;
}
if(p1==NULL) tail->next =p2;
else tail->next =p1;
return head;
}
List mult(List L1,List L2){
List head=NULL,p,tail;
List p1=L1,p2=L2;
while(p2){//初步结果
p=(List)malloc(sizeof(struct node));
p->next =NULL;
p->dishu =p1->dishu *p2->dishu ;
p->zhishu =p1->zhishu+p2->zhishu;
p2=p2->next ;
if(head==NULL)head=p;
else tail->next =p;
tail=p;
}
p1=p1->next ;
while(p1){
int equal;
p2=L2;////
while(p2){
List rear=head;
equal=0;
p=(List)malloc(sizeof(struct node));
p->next =NULL;
p->dishu =p1->dishu *p2->dishu ;
p->zhishu =p1->zhishu+p2->zhishu;
while(rear->next&&rear->next->zhishu >=p->zhishu ){
if(rear->next ->zhishu ==p->zhishu ){//相等
rear->next->dishu =p->dishu +rear->next ->dishu ;//
if(rear->next->dishu==0) rear->next =rear->next->next ; ////
equal=1;
break;
}
rear=rear->next ;
}
if(equal==0){//不相等
p->next =rear->next ;
rear->next =p;
}
p2=p2->next ;
}
p1=p1->next ;
}
return head;
}
合并同类项的语句哪里出错了?出错点在哪?