我正在使用指针、列表和结构。我有一个从 txt 文件中读取数据并将其保存在结构中的程序。将所有数据转换为节点后,我尝试打印列表。控制台中的输出就好像程序陷入了循环,永远只打印最后一个节点。
这是我的初始代码。我使用 initFromText() 打开 txt 文件并将每一行保存为一个节点。 在这个函数中,我调用了 enqueue(),它应该将新节点添加到列表的末尾,指向 NULL。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 35
typedef struct temperature{
float temperature;
struct temperature *next;
} Temperature;
typedef struct patient{
char name[25];
int age;
int room;
Temperature *firstTemp;
struct patient *next;
} Patient;
void initFromText(Patient **first);
void enqueue(Patient **first, Patient *newP);
void printPatients(Patient *first);
int main(){
Patient *first = NULL;
initFromText(&first);
printPatients(first);
return 0;
}
void initFromText(Patient **first){
FILE *file = fopen("data.txt", "r");
if (file == NULL) return;
char line[MAX_LINE_LENGTH];
Patient *newP = (Patient*)malloc(sizeof(Patient));
Temperature *newT = (Temperature*)malloc(sizeof(Temperature));
while(fgets(line, MAX_LINE_LENGTH, file)){
char *name = strtok(line, ",");
strcpy(newP->name, name);
newP->age = atoi(strtok(NULL, ","));
newP->room = atoi(strtok(NULL, ","));
newT->temperature = atof(strtok(NULL, ","));
newT->next = NULL;
newP->firstTemp = newT;
newP->next = NULL;
enqueue(first, newP);
}
fclose(file);
}
void enqueue(Patient **first, Patient *newP){
if (*first == NULL) *first = newP;
else{
Patient *current = *first;
while (current->next != NULL) current = current->next;
current->next = newP;
}
}
void printPatients(Patient *first){
if (first == NULL) printf("\n\n *No hay pacientes ingresados.\n");
else{
Patient *current = first;
do{
printf("\n********************");
printf("\n %s", current->name);
printf("\nEdad: %d", current->age);
printf("\nHabitacion: %d", current->room);
printf("\nTemperaturas:");
Temperature *currentTemp = current->firstTemp;
do{
printf("\n\t%.1f", currentTemp->temperature);
currentTemp = currentTemp->next;
}while(currentTemp != NULL);
current = current->next;
printf("\n********************");
}while(current != NULL);
}
}
在执行中,程序卡在了我提到的循环中。 我发现问题出在 enqueue() 函数中。我做了以下事情:
在 main() 中注释了 printPatients() 函数。
int main(){
Patient *first = NULL;
initFromText(&first);
//printPatients(first);
return 0;
}
注释了enqueue()函数,并在每次迭代中打印了newP的信息。每个节点都正确创建,指向 NULL。
void initFromText(Patient **first){
FILE *file = fopen("data.txt", "r");
if (file == NULL) return;
char line[MAX_LINE_LENGTH];
Patient *newP = (Patient*)malloc(sizeof(Patient));
Temperature *newT = (Temperature*)malloc(sizeof(Temperature));
while(fgets(line, MAX_LINE_LENGTH, file)){
char *name = strtok(line, ",");
strcpy(newP->name, name);
newP->age = atoi(strtok(NULL, ","));
newP->room = atoi(strtok(NULL, ","));
newT->temperature = atof(strtok(NULL, ","));
newT->next = NULL;
newP->firstTemp = newT;
newP->next = NULL;
printf("\n********************");
printf("\n %s", newP->name);
printf("\nEdad: %d", newP->age);
printf("\nHabitacion: %d", newP->room);
printf("\nTemperatura: %.1f", newP->firstTemp->temperature);
printf("\nSiguiente: %s", newP->next->name);
printf("\n********************");
//enqueue(first, newP);
}
fclose(file);
}
然后,我在 enqueue() 函数中重复了这个过程。结果是第一个节点被正确创建,指向 null,但是随后,每个节点都指向自己(2nd->next = 2nd,3rd->next = 3rd,等等)。显然,“第一个”指针最终指向最后一个节点。这样,程序末尾的循环将永远打印最后一个节点。
void enqueue(Patient **first, Patient *newP){
if (*first == NULL) *first = newP;
else{
Patient *current = *first;
while (current->next != NULL) current = current->next;
current->next = newP;
}
printf("\n********************");
printf("\n %s", newP->name);
printf("\nEdad: %d", newP->age);
printf("\nHabitacion: %d", newP->room);
printf("\nTemperatura: %.1f", newP->firstTemp->temperature);
printf("\nSiguiente: %s", newP->next->name);
printf("\n********************");
}
我已经尝试了很多东西,但无法理解问题所在。
我只需要移动以下两行代码:
Patient *newP = (Patient*)malloc(sizeof(Patient));
Temperature *newT = (Temperature*)malloc(sizeof(Temperature));
从这里 (initFromText())
char line[MAX_LINE_LENGTH];
Patient *newP = (Patient*)malloc(sizeof(Patient));
Temperature *newT = (Temperature*)malloc(sizeof(Temperature));
while(fgets(line, MAX_LINE_LENGTH, file)){
到这里
char line[MAX_LINE_LENGTH];
while(fgets(line, MAX_LINE_LENGTH, file)){
Patient *newP = (Patient*)malloc(sizeof(Patient));
Temperature *newT = (Temperature*)malloc(sizeof(Temperature));
这解决了我只创建 1 个节点并递归地将数据保存在其中的事实。现在它为 txt 文件的每一行创建一个节点。