You haven't said what you expect the code to do, but assuming that you want to insert a few nodes into a linked list and then print them out, how about:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node Node;
struct Node {
int data;
Node *next;
};
int main() {
Node *head = NULL;
for (int i = 0; i < 5; ++i) {
Node *nd = malloc(sizeof *nd);
nd->data = i * 10;
nd->next = head;
head = nd;
}
for (const Node *nd = head; nd; nd = nd->next)
printf("%d ", nd->data);
putchar('\n');
return 0;
}
It's a good idea to write a function to create the nodes:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node Node;
struct Node {
int data;
Node *next;
};
Node *new_node(int data, Node *next) {
Node *nd = malloc(sizeof *nd);
if (!nd) {
perror("new_node");
exit(EXIT_FAILURE);
}
nd->data = data;
nd->next = next;
return nd;
}
int main() {
Node *head = NULL;
for (int i = 0; i < 5; ++i)
head = new_node(i * 10, head);
for (const Node *nd = head; nd; nd = nd->next)
printf("%d ", nd->data);
putchar('\n');
return 0;
}