CH2_7.C
上传用户:lgb298
上传日期:2013-03-22
资源大小:1025k
文件大小:1k
- #include <alloc.h>
- #include <stdio.h>
- typedef struct node
- { int coef,exp;
- struct node *next;
- }JD;
- void add_poly(JD *pa,JD *pb)
- { JD *p,*q,*u,*pre;
- int x;
- p=pa->next;
- q=pb->next;
- pre=pa;
- while((p!=NULL) && (q!=NULL))
- { if(p->exp<q->exp)
- { pre=p; p=p->next;}
- else if(p->exp==q->exp)
- { x=p->coef+q->coef;
- if(x!=0){ p->coef=x; pre=p;}
- else { pre->next=p->next; free(p);}
- p=pre->next;
- u=q;
- q=q->next;
- free(u);
- }
- else
- { u=q->next;q->next=p;pre->next=q;
- pre=q; q=u;
- }
- }
- if(q!=NULL)
- pre->next=q;
- free(pb);
- }
- JD* dlbjl(int a[],int n)
- {
- JD *s,*h;
- int i;
- h=(JD*)malloc(sizeof(JD));
- h->coef=h->exp=-1;
- h->next=NULL;
- for(i=n;i>0;i--)
- {s=(JD*)malloc(sizeof(JD));
- s->coef=a[2*i-2];
- s->exp=a[2*i-1];
- s->next=h->next;
- h->next=s;
- }
- return (h);
- }
- void dlbxs(JD *h)
- { JD *p;
- p=h->next;
- while(p!=NULL)
- { printf("%d,%dt",p->coef,p->exp);
- p=p->next;
- }
- printf("n");
- }
- void main()
- { static int a[]={7,0,3,1,9,8,5,17};
- static int b[]={8,1,22,7,-9,8};
- JD *heada,*headb;
- int na=4,nb=3;
- heada=dlbjl(a,na);
- dlbxs(heada);
- headb=dlbjl(b,nb);
- dlbxs(headb);
- add_poly(heada,headb);
- dlbxs(heada);
- }