求助 同学说我的程序建立链表有问题
牛顿迭代法解一元多项式 运行了几次都是做到求导的地方就崩溃了#include<stdio.h>
#include<math.h>
#include<stdlib.h>
typedef struct Misaki{
double xishu;
double zhishu;
double DX;
double DZ;
Misaki *next;
}shi;
shi*creat()
{
double xi,zhi;
shi* head,*last,*work;
head=(shi*)malloc(sizeof(shi));
last=head;
printf("系数:");
scanf("%lf",&xi);
printf("指数:");
scanf("%lf",&zhi);
while(zhi!=0)
{
work=(shi*)malloc(sizeof(shi));
work->xishu=xi;
work->zhishu=zhi;
last->next=work;
last=work;
printf("系数:");
scanf("%lf",&xi);
printf("指数:");
scanf("%lf",&zhi);
}
last->next=NULL;
return head;
}
void qiudao(shi*head)
{shi*p;
p=head;
double newxi,newzhi;
if(head==NULL||head->next==NULL)
printf("没有导数\n");
else{
printf("导数为:");
while(p!=NULL)
{
newxi=p->xishu;
newzhi=p->zhishu;
if(newzhi==0)
{}
else{//求导
newxi=newzhi*newxi;
newzhi=newzhi-1;
printf("%.1lfx^%.1lf",newxi,newzhi);
printf("+");
p->DX=newxi;
p->DZ=newzhi;
p=p->next;}
}
newxi=p->xishu;
newzhi=p->zhishu;
if(newzhi==0)
{newzhi=0;
newxi=0;}
else{
newxi=newzhi*newxi;
newzhi=newzhi-1;
printf("%.1lfx",newxi);
printf("x^%.1lf\n",newzhi);
p->DX=newxi;
p->DZ=newzhi;
}
}
}
void Cal (shi*head)//设初始值为5
{
shi *p;
p=head;
double x=5;
double x0=0;
double xielv=0;
double xdot=0;
for(;;)
{
//求斜率
while(p!=NULL)
{
xielv+=p->DX*pow(x,p->DZ);
p=p->next;
}xielv+=p->next->DX*pow(x,p->next->DZ);
if(xielv==0)
{
printf("迭代过程中导数为0");
goto s;
}
//求多项式
p=head;
while(p!=NULL)
{
x0+=p->xishu*pow(x,p->zhishu);
}x0+=p->next->xishu*pow(x,p->next->zhishu);
//求交点坐标
xdot=x-(x0/xielv);
if(fabs(x0-xdot)<0.01)
break;
s:
x0=xdot;
}
printf("%lf",x0);
}
int main()
{
shi*head;
head=creat();
qiudao(head);
Cal (head);
return 0;
}