/* File: dyn_linked_list.c * Purpose: Illustrate how the linked list 5 -> 9 -> 7 can be built * using pointers and malloc * * Compile: gcc -g -Wall -o dyn_linked_list dyn_linked_list.c * Run: ./dyn_linked_list * * Input: None * Output: The ints in the list */ #include #include struct list_node_s { int data; struct list_node_s* next_p; }; void Print(struct list_node_s* head_p); int main(void) { struct list_node_s* head_p; struct list_node_s* temp_p; temp_p = malloc(sizeof(struct list_node_s)); temp_p->data = 7; temp_p->next_p = NULL; head_p = temp_p; temp_p = malloc(sizeof(struct list_node_s)); temp_p->data = 9; temp_p->next_p = head_p; head_p = temp_p; temp_p = malloc(sizeof(struct list_node_s)); temp_p->data = 5; temp_p->next_p = head_p; head_p = temp_p; Print(head_p); return 0; } /* main */ /*----------------------------------------------------------------- * Function: Print * Purpose: Print list on a single line of stdout * Input arg: head_p */ void Print(struct list_node_s* head_p) { struct list_node_s* curr_p = head_p; printf("list = "); while (curr_p != NULL) { printf("%d ", curr_p->data); curr_p = curr_p->next_p; } printf("\n"); } /* Print */