#include <stdio.h> struct node { int i; struct node *next; }; int main(void) { struct node* temp, *list = NULL; for(i=0;i<10;i++) { temp = (struct node *) malloc(sizeof(struct list)); temp->i = i; temp->next = list; list = temp; } // do something with list and free nodes later return 0; }
This is extremely common code in C programs. Freeing the list is easy because all nodes are hanging together via the next pointer.