Are you all doing the same course? If there are only 3 nodes you'd be better off using an array. Or, if you want to simulate a linked list just do:
Code:
#include <stdio.h>
typedef struct node {
int n;
struct node *next;
} node;
int main(void)
{
node list[] = {
{ 1, &list[1] }, /* head */
{ 2, &list[2] }, /* silly */
{ 3, NULL } /* tail */
};
for (node *curr = list; curr; curr = curr->next) {
printf("%d\n", curr->n);
}
return 0;
}
Not much point in doing that though except to, perhaps, see how linked lists work
I guess what I'm saying is that unless you're using functions to construct the list there is essentially no point in having a linked list. By the way, in your version you're not freeing the memory you allocated using malloc(), so that's a problem
Or, if you want to be really cunning
Code:
#include <stdio.h>
typedef struct node {
int n;
struct node *next;
} node;
int main(void)
{
node list[] = { { 1, &list[2] }, { 3, NULL }, { 2, &list[1] } };
for (node *curr = list; curr; curr = curr->next)
printf("%d\n", curr->n);
return 0;
}