多项式加法:
#include"stdio.h"
#include"stdlib.h"
struct node
{
int cof,exp;
struct node *next;
};
struct node *create()
{
struct node *head,*p,*q;
int i,j;
head=(struct node *)malloc(sizeof(struct node));
q=head;
printf("input i,j:");
scanf("%d%d",&i,&j);
while(i!=-999&&j!=-999)
{
p=(struct node *)malloc(sizeof(struct node));
p->cof=i;
p->exp=j;
q->next=p;
q=p;
scanf("%d%d",&i,&j);
}
q->next=NULL;
return(head);
}
void outline(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
printf("%5d%5d",p->cof,p->exp);
p=p->next;
}
void lineadd(struct node *head1,struct node *head2)
{
struct node *p,*q,*r;
int x;
int cof,exp;
p=head1->next;
q=head2->next;
r=head1;
while(p!=NULL&&q!=NULL)
{
if(p->exp==q->exp)
{
x=p->cof+q->cof;
if(x!=0)
{
p->cof=x;
r->next=p;
r=p;
}
p=p->next;
q=q->next;
}
else if(p->exp>q->exp)
{
r->next=p;
r=p;
p=p->next;
}
else
{
r->next=q;
r=q;
q=q->next;
}
}
if(p==NULL)
r->next=q;
if(q==NULL)
r->next=p;
}
main()
{
struct node *head1,*head2;
head1=(struct node *)malloc(sizeof(struct node));
head2=(struct node *)malloc(sizeof(struct node));
head1=create();
head2=create();
lineadd(head1,head2);
outline(head1);
}
高手指点我的程序有没有可能可以改的更好一点。