#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);
}
c语言链表插入函数有错,输出无限循环 为什么啊??
答案:2 悬赏:10
解决时间 2021-04-07 01:48
- 提问者网友:护她一生,唯爱
- 2021-04-06 20:52
最佳答案
- 二级知识专家网友:零负荷的放任
- 2021-04-06 21:52
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;
为什么这么改,请仔细看 东郭努谊的回复
在struct node *insert(struct node *head,struct node stu)函数里,把第二行 p0=&stu;注释掉,然后加两行,即:
//p0=&stu;
p0=(struct node *)malloc(sizeof(struct node));
p0->num=stu.num;
为什么这么改,请仔细看 东郭努谊的回复
全部回答
- 1楼网友:星星坠落
- 2021-04-06 21:58
当然是没有用的 因为
q->next=null;
q->next=p->next;
这两句话后面一句就把前面那句覆盖了
完全可以删除掉第一句
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯