c语言编写的程序出现停止运行的问题
我编写的c程序是一元多项式相加的问题,在运行的时候出现停止程序运行的情况,在de-bug之后发现在下边的AddLink部分出现问题,如果只进行第一步最开始加的那一块没问题,加上while之后的那部分问题就出现了。# include<stdio.h>
# include<stdlib.h>
struct node{
int Exp;
int Coef;
struct node *link;
} ;
void SetupLink(struct node **H, int e, int c){
struct node *P, *Q, *X;
P = (struct node *)malloc(sizeof(struct node));
P->Exp = e;
P->Coef = c;
if (*H != NULL)
(*H)->link = P;
else
*H = P;
}
void AddLink(struct node **CH, struct node **P, struct node **Q){ //应该是这部分出问题了
struct node *d=0;
int e,c;
if ((*P)->Exp == (*Q)->Exp){
e = (*P)->Exp;
c = (*P)->Coef+(*Q)->Coef;
SetupLink(&d,e,c);
*P = (*P)->link;
*Q = (*Q)->link;
*CH = d;
}
else if ((*P)->Exp >(*Q)->Exp){
e = (*Q)->Exp;
c = (*Q)->Coef;
SetupLink(&d,e,c);
*Q = (*Q)->link;
*CH = d;
}
else {
e = (*P)->Exp;
c = (*P)->Coef;
SetupLink(&d,e,c);
*P = (*P)->link;
*CH = d;
}
while (((*P)->link != NULL)&&((*Q)->link != NULL ) ){
if ((*P)->Exp == (*Q)->Exp){
e = (*P)->Exp;
c = (*P)->Coef+(*Q)->Coef;
SetupLink(&d,e,c);
*P = (*P)->link;
*Q = (*Q)->link;
d = d->link;
}
else if ((*P)->Exp >(*Q)->Exp){
e = (*Q)->Exp;
c = (*Q)->Coef;
SetupLink(&d,e,c);
*Q = (*Q)->link;
d = d->link;
}
else {
e = (*P)->Exp;
c = (*P)->Coef;
SetupLink(&d,e,c);
*P = (*P)->link;
d = d->link;
}
}
if ((*P)!=NULL){
while ((*P)!=NULL){
e = (*P)->Exp;
c = (*P)->Coef;
SetupLink(&d,e,c);
*P = (*P)->link;
d = d->link;
}
}
else{
while ((*Q)!=NULL){
e = (*Q)->Exp;
c = (*Q)->Coef;
SetupLink(&d,e,c);
*Q = (*Q)->link;
d = d->link;
}
}
}