我只是在摆弄链接列表,但似乎我脑子里有很多问题。我已经用函数尝试过这个,但我无法让它工作。我以为我会通过使它更简单来解决它,但它仍然不起作用。它通常只打印第一个元素,然后是随机的大数字(最有可能的内存地址)
我只想创建一个列表并打印其内容。
#include <stdio.h>
#include <stdlib.h>
struct el{
int value;
struct el *next;
};
typedef struct el Elem;
int main()
{
int nr, i;
struct el *Head, *Conductor;
Head = malloc(sizeof(Elem));
Conductor = Head;
printf("How many elements do you want to add? ");
scanf("%d", &nr);
for(i = 0; i < nr; i++)
{
printf("Enter value for element %d: ", i);
scanf("%d", &(Conductor->value));
Conductor = Conductor->next;
Conductor = malloc(sizeof(Elem));
}
free(Conductor->next);
Conductor->next = NULL;
Conductor = Head;
printf("\n");
for(i = 0; i < nr; i++)
{
printf("%d -> ", Conductor->value);
Conductor = Conductor->next;
}
return 0;
}