请问一下,运行结果为什么不是1234而是0012啊,能帮忙改一下吗
#include <stdio.h>
#include <stdlib.h>
typedef struct pnode
{
float coef;//系数
int expn;//指数
struct pnode *next;//指向下一项的指针
}pnode;//一元多项式中的项
int main()
{
pnode *creatpoly( int n);
void output(pnode *head);
int n;
pnode *heada;
printf("请输入一元多项式A的项数:");
scanf("%d",&n);
heada=creatpoly(n);
output(heada);
return 0;
}
pnode *creatpoly(int n)
{
int i;
pnode *head,*s,*pre;
head=(pnode*)malloc(sizeof(pnode));
head->next=NULL;
pre=head;
for(i=0;i<n;i++)
{
s=(pnode*)malloc(sizeof(pnode));
scanf("%f",&s->coef);
scanf("%d",&s->expn);
s->next=NULL;
while(pre->next!=NULL)
{
pre=pre->next;
}
pre->next=s;
}
return head;
}
void output(pnode *head)
{
pnode *s=head;
while(s->next!=NULL)
{
printf("%.0f %d ",s->coef,s->expn);
s=s->next;
}
}
请问一下,运行结果为什么不是1234而是0012啊,能帮忙改一下吗
错误在output函数,有两个错误
1.这里建立的链表有附加头结点,这个节点也被输出了,而头结点里面是没有存值的,所以输出是0 0。
s定义应改为pnode*s=head->next;
2.最后一个节点没有输出,因为while的循环条件错误,应该是只要s不是NULL就输出,而不是s->next