链表输入的时候出现问题了。怎么修改,是为了帮一个链表排序的,输入系数和指数
#include<stdio.h>#include<malloc.h>
typedef struct pnode{
double coef;
int exp;
struct pnode *next;
}linklist;
void print(linklist *L)
{
linklist *p=L->next;
while(p!=NULL)
{
if(p->coef==0)
;
else if(p->exp==0)
printf("%lf 0",p->coef);
else
printf("%lf %d",p->coef,p->exp);
p=p->next;
}
}
void destroylist(linklist *L)
{
linklist *p=L,*q=L->next;
while(q!=NULL)
{
free(p);
p=q;
q=p->next;
}
}
void creat(linklist *L)
{
L=(linklist *)malloc(sizeof(linklist));
L->next=NULL;
}
int Insert (linklist * L,double coef,int exp)
{
linklist *p,*s;
p=L;
s=(linklist *)malloc( sizeof (linklist));
s->next = NULL;
if (!s)
return 0;
s->coef=coef;
s->exp=exp;
s->next=p->next;
p->next=s;
return 1;
}
void sort(linklist *&head)
{
linklist *p=head->next,*q,*r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;//构造只含有一个结点的有序表
p=r;
while(p!=NULL)
{
r=p->next;//r保存*P结点后继结点的指针
q=head;
while(q->next!=NULL && q->next->exp>p->exp)
q=q->next; //在有序表中找插入*P的前驱结点*q
//将*p插入到*q之后
p->next=q->next;
q->next=p;
p=r;
}
}
}
int main()
{
linklist *ha=NULL;
int ch;
creat(ha);
while((ch=scanf_s("%lf %d",&ha->coef,&ha->exp))==2 && ha->exp > 0 )
{
Insert(ha,ha->coef,ha->exp);
}
print(ha);
return 0;
}