链表问题 快来大佬帮忙啊
大佬们 我直接把想问的问题用注释形式写出来#include <stdio.h>
#include <stdlib.h>
struct Node
{
int a;
struct Node *next;
};
void insertNode(struct Node **head,int a)
{
struct Node *p;
struct Node *dangqian;
struct Node *now;
dangqian=*head;
p=NULL;
while(dangqian!=NULL&&dangqian->a<a) //为什么下面input传入的数字跑到dangqian里面去了咋进去的啊
{
p=dangqian;
dangqian=dangqian->next;
}
now=(struct Node *)malloc(sizeof(struct Node));
if(now==NULL)
{
exit(1);
}
now->a=a;
now->next=dangqian;
if(p==NULL)
{
*head=now;
}
else
{
p->next=now;
}
}
void printNode(struct Node *head)
{
struct Node *dangqian;
dangqian=head;
while(dangqian!=NULL)
{
printf("%d ",dangqian->a);
dangqian=dangqian->next;
}
putchar('\n');
}
int main()
{
struct Node *head=NULL;
int input;
while(1)
{
printf("请输入一个整数 按-1退出");
scanf("%d",&input);
if(input==-1)
{
break;
}
insertNode(&head,input);
printNode(head);
}
} while(dangqian!=NULL&&dangqian->a<a) //为什么下面input传入的数字跑到dangqian里面去了咋进去的啊
{
p=dangqian;
dangqian=dangqian->next;
}
没明白想问什么,这个循环语句有毛病。dangqian->a是个未初始化的值,把它当做循环条件结果不可知。不知道这个循环的目的是啥
页:
[1]