为什么我的链表在遍历时会跳过第一个节点?
Why is my linked list when being traversed skipping the first node?
我正在学习 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 %f\n",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[=11=] 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
为什么打印不出第一人称?
创建第一个节点后,循环会在每次迭代时创建一个新节点,但它将用户的输入分配给列表中的 previous 节点,而不是当前 节点正在添加到列表中。因此,在第一次迭代中,您将覆盖第一个节点的数据,然后附加第二个空节点。然后在第 2 次迭代中,您将覆盖第 2 个节点的数据,并附加一个空白的第 3 个节点。等等。
试试这个:
#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 %f\n", 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;
}
我正在学习 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 %f\n",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[=11=] 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
为什么打印不出第一人称?
创建第一个节点后,循环会在每次迭代时创建一个新节点,但它将用户的输入分配给列表中的 previous 节点,而不是当前 节点正在添加到列表中。因此,在第一次迭代中,您将覆盖第一个节点的数据,然后附加第二个空节点。然后在第 2 次迭代中,您将覆盖第 2 个节点的数据,并附加一个空白的第 3 个节点。等等。
试试这个:
#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 %f\n", 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;
}