#include #include //for malloc(3) #include //for mmap, brk, sbrk struct node { char data; long num; struct node* link; }; struct node list_head; /** * Place 'tmp' onto the rear of the list. * * Lame, linear implementation. */ void insert(struct node* tmp) { struct node *itr = NULL; itr = &list_head; while(NULL!=itr->link) { itr = itr->link; } //now itr points to the "last" node on the list. //update last->link to be tmp itr->link = tmp; //tmp->link should already be NULL (via create_node()) return; } void print_list() { struct node *itr = NULL; itr = &list_head; while(NULL!=itr) { if(itr->link) { fprintf(stdout, "[%c,%ld,%p]->", itr->data, itr->num, itr->link); }else{ fprintf(stdout, "[%c,%ld,%s]", itr->data, itr->num, "/"); } itr = itr->link; } fprintf(stdout, "\n"); return; } void create_node(long i) { struct node *tmp = NULL; tmp = malloc(sizeof(struct node)); if(NULL == tmp) { fprintf(stderr, "failed to allocated space for new node\n"); abort(); } tmp->data = 'M'; tmp->num = i; tmp->link = NULL; insert(tmp); tmp = NULL; //not needed, but semantically clears the pointer return; } void do_work() { long i = 0; for(;;) { create_node(i); i++; print_list(); sleep(1); } } /** * Create some number of linked list nodes. */ int main(int argc, char* argv[]) { /* 'list_head' has been created for us by the compiler and placed * into the .data section of the ELF. Let's initialize its values. * The rest of the members of the list will exist on the heap. */ list_head.data = 'M'; list_head.num = -1; list_head.link = NULL; fprintf(stdout, "my pid = %d\n", getpid()); fprintf(stdout, "size of struct node is %d\n", sizeof(struct node)); do_work(); return 0; }