C语言链表改错,高手进!

#include<stdio.h>#include<stdlib.h>structnode{intcoef;intexp;structnode*next;};struct... #include <stdio.h>
#include <stdlib.h>
struct node
{
int coef;
int exp;
struct node *next;
};
struct node *creat()
{
struct node *p,*head,*s;
int a;
p=head=(struct node *)malloc(sizeof (struct node));
while(p!=NULL)
{
s=(struct node *)malloc (sizeof(struct node));
printf("\n请 输 入 系数\t");
scanf("%d",&s->coef);
printf("请 输 入 指数\t");
scanf("%d",&s->exp);

p->next=s;
p=s;
printf("\t\t\t 按 1 继续输入,按 2 结束输入");
scanf("%d",&a);
if(a==2)break;
}
p->next=NULL;
return head;
}
struct node *print(struct node *head)
{
struct node *p;
p=head->next;

printf("\n函数=");
while(p!=NULL)
{
if(p->exp==0)
printf("%3d ",p->coef);
else
printf("%3dx^%3d ",p->coef,p->exp);
if(p->next !=NULL)
printf("+");
p=p->next;
}
printf("\n\n");
}
struct node *together(struct node *head1,struct node *head2)
{
struct node *p,*q,*pr;

pr=head1;
p=head1->next;
q=head2->next ;
while (p!=NULL&&q!=NULL)
{
if(p->exp<q->exp)
{pr->next =p;
pr=p;
p=p->next ;
}//if

if(p->exp >q->exp )
{
pr->next =q;
pr=q;
q=q->next;
}//if
else
{

p->coef=p->coef+q->coef;
pr->next =p;
pr=p;
p=p->next;
q=q->next;
}//if
}//while
if(p!=NULL)
pr->next=p;
else
pr->next=q;

return head1;
}

main()
{
struct node *head1 ,*head2,*head;
printf("输入函数A 例如:A=4+6x^3+5x^8+4x^12\n");
head1=creat();
print(head1);
printf("\n输入函数B 例如:B=5+2x^4+6x^7+3x^12\n");
head2=creat();
print(head2);
head=together(head1,head2);
printf("合并后:");
print(head);

}

为什么1:当我在函数一输入 1 1 2 2,函数2输入 5 0 4 4 合并后错误呢?
2 :当我在函数一:输入 1 1 函数2 输入 2 2 ,运行不了?
而我输入例如的函数时,合并的答案是正确呢?
展开
 我来答
zhouciming
2012-03-12 · TA获得超过223个赞
知道小有建树答主
回答量:393
采纳率:0%
帮助的人:178万
展开全部
单步调试几乎能解决95%以上的问题!
更多追问追答
追问
不会调试···帮我看看程序好不?
追答
多调试下就会了。网上找教程吧。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式