gpt4 book ai didi

c - 打印出链表的输入 [C]

转载 作者:行者123 更新时间:2023-11-30 15:07:58 26 4
gpt4 key购买 nike

我编写了一个链接列表并设法向其中添加文件支持。现在命令提示符输出有问题。最后的 pokemon->namepokemon->number 输出很神秘。不知何故,我认为我在将最后一批数据保存到内存中时犯了一个错误,因为它实际上是正确保存到文件中的。

这是代码(代码后输入测试)

pokemonPtr addPokemon(void){

FILE *filePtr;

//filePtr = fopen ("pokedex.txt", "a");

//if (filePtr == NULL){
filePtr = fopen ("pokedex.txt","w");
//}

pokemonPtr firstPtr;
pokemonPtr thisPokemon;
firstPtr = NULL;

firstPtr = (pokemon *) malloc(sizeof(pokemon));
firstPtr->name = (char *) malloc(sizeof(char) * POKEMON_LENGTH);

printf ("Enter the name of the Pokemon.\n");
scanf("%s",firstPtr->name);
fprintf(filePtr, "Pokemon Name:%s ", firstPtr->name);
getchar();
printf ("Enter the number of the Pokemon.\n");
scanf("%d",&firstPtr->number);
fprintf(filePtr, "Pokemon Nummer:%d\n", firstPtr->number);



firstPtr->next = (pokemon *) malloc(sizeof(pokemon));

thisPokemon = firstPtr->next;

int i = 0;

while (i < 2){

thisPokemon->name = (char *) malloc(sizeof(char) * POKEMON_LENGTH);

printf ("Enter the name of the Pokemon.\n");
scanf("%s",thisPokemon->name);
fprintf(filePtr, "Pokemon Name:%s ", thisPokemon->name);
printf ("Enter the number of the Pokemon.\n");
scanf("%d",&thisPokemon->number);
fprintf(filePtr, "Pokemon Nummer:%d\n", thisPokemon->number);

thisPokemon->next =(pokemon *) malloc (sizeof(pokemon));
thisPokemon = thisPokemon->next;

i++;

}

thisPokemon->next = NULL;

fclose (filePtr);

return firstPtr;

}

void showPokemon(pokemonPtr firstPtr){

printf ("Name: %s\n"
"Nummer: %d\n", firstPtr->name, firstPtr->number);

pokemonPtr thisPokemon = firstPtr->next;

while (thisPokemon != NULL){

printf ("Name: %s\n"
"Nummer: %d\n", thisPokemon->name, thisPokemon->number);

thisPokemon = thisPokemon->next;
}


}

我尝试的输入是:

Pokemon Name:dudu Pokemon Nummer:3
Pokemon Name:dada Pokemon Nummer:3
Pokemon Name:dudi Pokemon Nummer:23
The output in cmd was:
Name: dudu
Nummer: 3
Name: dada
Nummer: 3
Name: dudi
Nummer: 23
Name: Ót
Nummer: 7607776

这里发生了什么?

最佳答案

好吧,问题出在你构建链接列表的方式上 - 一旦你在调试器下执行代码,它就变得微不足道了 -> 你真的应该学会这样做;-)

您正确地将头部保存在 firstPtr 中,但您以空但已创建的链接元素开始循环。所以这是 addPokemon 中发生的事情:

  • 初始部分:分配一个新项目并读取其值分配一个新项目并将其链接到第一个:first(dudu) -> this(empty)
  • 第一次传入循环:first(dudu) -> dada ->empty
  • 循环中的第二次传递:first(dudu) -> dada -> dudi ->empty
  • 循环结束后,你输入了一个 null,但一步太远了:first(dudu) -> dada-> dudi ->empty -> NULL

如何修复:

您应该在将值存储在循环中之前在循环中分配一个新项目,并使用 thisPokemon 指向firstPokemon 来启动循环:

...
firstPtr->next = NULL;

thisPokemon = firstPtr;

int i = 0;

while (i < 2){

thisPokemon->next =(pokemon *) malloc (sizeof(pokemon));
thisPokemon = thisPokemon->next;
thisPokemon->name = (char *) malloc(sizeof(char) * POKEMON_LENGTH);

printf ("Enter the name of the Pokemon.\n");
scanf("%s",thisPokemon->name);
fprintf(filePtr, "Pokemon Name:%s ", thisPokemon->name);
printf ("Enter the number of the Pokemon.\n");
scanf("%d",&thisPokemon->number);
fprintf(filePtr, "Pokemon Nummer:%d\n", thisPokemon->number);

i++;

}
...

关于c - 打印出链表的输入 [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37774119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com