下面是我编的用循环双向链表进行一元多项式相乘的程序,运行不出, 哪位高手能帮我看一下,谢谢!!! #include<stdio.h> #include<stdlib.h> typedef int datatype; typedef struct DuLNode{ datatype data; datatype expn; struct DuLNode *prev; struct DuLNode *next; }DuLNode,*DuLinkList;
DuLNode L,N,C;
DuLNode * insertdu(DuLNode *L,int n) {DuLNode *s; int i; L=(DuLNode *)malloc(sizeof(DuLNode)); if(L==NULL) {printf("OVERFLOW!\n");exit(1);} L->data=0; L->prev=L; L->next=L; for(i=0;i<=n-1;i++) {s=(DuLNode *)malloc(sizeof(DuLNode)); if(s==NULL) {printf("OVERFLOW!\n");exit(1);} scanf("%d",&s->data); scanf("%d",&s->expn); s->next=L; L->prev->next=s; s->prev=L->prev; L->prev=s;} return(L); }
DuLNode * multiply(DuLNode *L,DuLNode *N,DuLNode *C) {DuLNode *p,*q,*s,*r; C=(DuLNode *)malloc(sizeof(DuLNode)); if(C==NULL) {printf("OVERFLOW!\n");exit(1);} C->data=0; C->expn=0; C->next=C; C->prev=C; q=N->next; while(q!=N) {p=L->next; while(p!=L) {s=(DuLNode *)malloc(sizeof(DuLNode)); if(s==NULL) {printf("OVERFLOW!\n");exit(1);} s->data=q->data*p->data; s->expn=q->expn+p->expn; r=C->next; while(r!=C) {if(s->expn==r->expn) {r->data=r->data+s->data;break; } else if(s->expn<r->expn) r=r->next; else {s->next=r; r->prev->next=s; s->prev=r->prev; s->prev=r; break;}} if(r==C) {s->next=C; C->prev->next=s; s->prev=C->prev; C->prev=s;} p=p->next;} q=q->next;} return(C); }
void printdu(DuLNode *L) {DuLNode *p; p=L->next; while(p!=L) {printf("%d^%d ",p->data,p->expn); p=p->next;} printf("\n"); }
void main(void) {DuLNode A,B; DuLNode *headA,*headB,*headC; int n; printf("以下数据请您按照指数从大到小的输入!!!\n\n"); printf("[[["); printf("3^3表示指数为3,系数为3的项"); printf("]]]\n\n"); printf("请输入您需要参加相乘运算的第一个多项式:\n"); printf("您的第一个多项式的项数为: n="); scanf("%d",&n); printf("请输入数据:\n"); headA=insertdu(&A,n); printf("第一个多项式为:\n"); printdu(headA); printf("请输入您需要参加相乘运算的第二个多项式:\n"); printf("您的第二个多项式的项数为:\n"); scanf("%d",&n); printf("请输入数据:\n"); headB=insertdu(&B,n); printf("第二个多项式为:\n"); printdu(headB); headC=multiply(&A,&B,&C); printdu(headC); }