int count = 0;
struct Student {
char name[100];
int number;
struct Student* next;
};
struct Student* Creat()
{
struct Student* head = NULL;
struct Student* end, * new1;
end = new1 = (struct Student*)malloc(sizeof(struct Student));
printf("请输入姓名\n");
scanf("%s", &new1->name);
while (new1->name!=NULL)//为什么我输入为空,跳不出循环,怎么输入才能跳出循环啊
{
count++;
if (count == 1)
{
new1->next = head;
head = new1;
end = new1;
}
else
{
new1->next = NULL;
end->next = new1;
end = new1;
}
new1 = (struct Student*)malloc(sizeof(struct Student));
scanf("%s", &new1->name);
}
free(new1);
return head;
}
void print(struct Student* head)
{
struct Student* temp;
temp = head;
int n = 1;
printf("--有%d个成员:----\n", count);
printf("\n");
while (temp != NULL)
{
printf("NO:%d成员:\n", n);
printf("姓名:%s\n", temp->name);
printf("\n");
temp = temp->next;
n++;
}
}
int main()
{
struct Student* head;
head = Creat();
print(head);
return 0;
}
各位看看这个链表的循环怎么结束
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
快乐鹦鹉 2023-03-29 21:00关注new1->name!=NULL 永远不会是空啊
改为:
while(strlen(new1->name) == 0)
或者while(new1->name[0] != '\0')本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用