I'm just messing around with linked lists but it seems I have many things wrong in my mind. I've tried this with functions and I couldn't get it working. I thought I'd get it right by making it simplier but it still doesn't work. It usually prints just the first element right and then random huge numbers (memory addresses most likely)
I just want to create a list and print its content.
#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;
}