我正在学习C,目前正在学习动态内存分配和链表,遇到了一个问题。我找到了这篇帖子,并按照答案做了回答。然而,当打印出来时,我的程序仍然跳过我的第一个节点。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record
{
int id;
char name[257];
float age;
struct record *next;
};
int main(void)
{
int i = 1;
struct record *firstrecord = (struct record *)malloc(sizeof(struct record));
firstrecord->id = 1000;
char firstname[] = "John Doe";
memcpy(firstrecord->name, firstname,strlen(firstname) + 1);
firstrecord->age = 24.5;
firstrecord->next = NULL;
struct record *mostrecentlycreated = firstrecord;
int iyes = 1;
char cyes;
int yes = 1;
while (yes == iyes)
{
// build next node
mostrecentlycreated->next = (struct record *)malloc(sizeof(struct record));
// change data in new record
int newid;
char newname[257];
char temp;
float newage;
char cnewage[257];
printf("Enter the name: ");
fgets(newname,257,stdin);
newid = 1000 + i;
printf("Enter the person's age: ");
fgets(cnewage,257,stdin);
newage = atof(cnewage);
mostrecentlycreated->id = newid;
memcpy(mostrecentlycreated->name,newname,strlen(newname) + 1);
mostrecentlycreated->age = newage;
mostrecentlycreated = mostrecentlycreated->next;
mostrecentlycreated->next = NULL;
i++;
// decide whether or not to keep going
printf("Do you wish to continue? 1\0 ");
fgets(&cyes,1,stdin);
iyes = atoi(&cyes);
scanf("%c",&temp);
}
struct record *traverse = firstrecord;
while (traverse != NULL)
{
printf("Employee id is %i; Employee Name is %s; Employee Age is %fn",traverse->id,traverse->name,traverse->age);
traverse = traverse->next;
}
free(firstrecord);
free(mostrecentlycreated);
return 0;
}
这是我的输出:
Enter the name: john doe
Enter the person's age: 23
Do you wish to continue? 1 0
Employee id is 1001; Employee Name is john doe
; Employee Age is 23.000000
Employee id is 0; Employee Name is ; Employee Age is 0.000000
为什么它不打印第一人称?
创建第一个节点后,循环在每次迭代中都会创建一个新节点,但它会将用户的输入分配给列表中的前一个节点,而不是添加到列表中的当前。因此,在第一次迭代中,覆盖第一个节点的数据,然后附加第二个空节点。然后在第二次迭代中,覆盖第二个节点的数据,并附加一个空白的第三个节点。等等。
试试这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record
{
int id;
char name[257];
float age;
struct record *next;
};
int main(void)
{
int i = 1;
struct record *firstrecord = (struct record *)malloc(sizeof(struct record));
firstrecord->id = 1000;
strncpy(firstrecord->name, "John Doe", 257);
firstrecord->age = 24.5;
firstrecord->next = NULL;
struct record *mostrecentlycreated = firstrecord;
const int cyes = 1;
int iyes = 1;
while (iyes == cyes)
{
// build next node
struct record *newrecord = (struct record *)malloc(sizeof(struct record));
// change data in new record
newrecord->id = 1000 + i;
printf("Enter the name: ");
fgets(newrecord->name, 257, stdin);
printf("Enter the person's age: ");
scanf("%f", &(newrecord->age));
newrecord->next = NULL;
mostrecentlycreated = newrecord;
++i;
// decide whether or not to keep going
printf("Do you wish to continue? 1\0 ");
scanf("%d", &iyes);
}
struct record *traverse = firstrecord;
while (traverse != NULL)
{
printf("Employee id is %i; Employee Name is %s; Employee Age is %fn", traverse->id, traverse->name, traverse->age);
traverse = traverse->next;
}
traverse = firstrecord;
while (traverse != NULL)
{
struct record *nextrecord = traverse->next;
free(traverse);
traverse = nextrecord;
}
return 0;
}