中易网

c语言链表插入函数有错,输出无限循环 为什么啊??

答案:2  悬赏:10  
解决时间 2021-04-07 01:48
#include <stdio.h>
#define NULL 0
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
struct node *creat()
{
struct node *head=NULL,*p1,*p2;
int n=0;
p1=p2=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p1->num);
while(p1->num!=NULL)
{
n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p1->num);
}
p2->next=NULL;
return head;
}
void put(struct node *head)
{
struct node *p;
p=head;
if(head!=NULL)
while(p!=NULL)
{
printf("%d",p->num);
p=p->next;
}
}
struct node *insert(struct node *head,struct node stu)
{
struct node *p1,*p2,*p0;
p0=&stu;
p1=head;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
void main()
{
struct node *head,stu;

head=creat();
put(head);
printf("\n");
scanf("%d",&stu.num);
head=insert(head,stu);
put(head);
}
最佳答案
LZ,我要认真的告诉你,东郭努谊 说的很对,他的回复除了格式不好看外,里面说的很对,我可以告诉最简单的改正你程序方法
在struct node *insert(struct node *head,struct node stu)函数里,把第二行 p0=&stu;注释掉,然后加两行,即:
//p0=&stu;
p0=(struct node *)malloc(sizeof(struct node));
p0->num=stu.num;
为什么这么改,请仔细看 东郭努谊的回复
全部回答

当然是没有用的 因为

q->next=null;
q->next=p->next;
这两句话后面一句就把前面那句覆盖了

完全可以删除掉第一句

我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
昆仑润滑油15w一40机油好不好
十年前的桑塔纳裸车价多少钱大概
沈阳哪家 牛排比较好吃 比较有名!
三星smg9900f手机后盖
九莲坊地址在什么地方,想过去办事
为什么女人生完孩子后容易生气
mathtype 6.0如何插入到word界面中
之前买的黄金戒指手镯现在打算卖了,去什么地
为了天界的和平 二图任务怎么过
版权登记能不能被工作人员剽窃 一般费用是多
云存储该怎么选?云服务优势劣势大比拼
名词解释:合同 问答题:什么是“三线一面”
请问三星ML 1510激光打印机用什么墨粉比较合
一共26字母 有个字只需要五个字母组成 那有好
笔记本电脑用哪个wifi解锁器软件比较好?有不
推荐资讯
在淘宝上开一家蒸笼店怎么样
震德注塑机参数
魅族M578CA为什么没有指纹解锁啊,求解
新加坡的消费水平怎么样
良山镇下保村民委员会在哪里啊,我有事要去这
请问这个乐器是什么名字
忘了赶集网的注册名密码还有注册邮箱怎么删除
桂花土菜馆地址在什么地方,想过去办事
美亚直邮,包裹已经到了国内。 但是承运公司
高一语文必修三《蜀道难》课后练习题第四题。
千艺发型工作室地址在哪,我要去那里办事
作文和同学一起在学校种花
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?