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;
}
new1->name!=NULL 永远不会是空啊
改为:
while(strlen(new1->name) == 0)
或者while(new1->name[0] != '\0')
方法一:while(strlen(new1->name) == 0)
方法二:while(new1->name[0] != '\0')
一种受限制的线性表,栈底为高地址,栈顶为低地址。
这么改,供参考:
#include <stdio.h>
#include <stdlib.h>
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 (gets_s(new1->name) != NULL)// 输入 ctrl + z 结束输入 修改
{
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;
}
while (strlen(new1->name)!=0)