运行以下代码时我的系统挂起。我试图了解链表和链表操作的基础知识。有人可以向我解释我做错了什么(不明白)。谢谢大家。
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNodeT
{
float power;
struct ListNodeT *nextPtr;
}ListNodeType;
void ReadFileList(ListNodeType *Data);
int main(void)
{
ListNodeType a;
ReadFileList(&a);
ListNodeType *node = &a;
do
{
printf("%f", node->power);
node = node->nextPtr;
}while(node != NULL);
return EXIT_SUCCESS;
}
void ReadFileList(ListNodeType *Data)
{
ListNodeType new[2];
Data->nextPtr = &new[0];
new[0].nextPtr = &new[1];
new[1].nextPtr = NULL;
Data->power = 0.1;
new[0].power = 1.2;
new[1].power = 2.3;
}
最佳答案
您正在用指向局部变量的指针填充 ReadFileList
中的 Data
。当 ReadFileList
返回时,它们超出范围,因此您会导致未定义的行为。
关于c - 打印链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414913/