// Week 7 Thursday Lecture // Create a list with nodes by adding nodes with data 0..9 to the beginning // of an empty list // Use a functions to create nodes // Use a loop and a function to print nodes // Test the print function with an empty list // Use a loop to insert nodes at the tail of a new list // We will leave the function to free nodes until later // For now we will have a memory leak in this program #include #include struct node { int data; struct node *next; }; struct node *create_node(int data, struct node *next); void print_list(struct node *head); void insert_at_tail(struct node *head, int data); int main(void) { // Declare and initialise our list // Right now this is an empty list but we will add nodes to it struct node *head = NULL; // Temporary variable to hold new nodes struct node *new_node = NULL; //create a node with data value i using function //add node to beginning of the list for (int i = 0; i < 10; i++) { new_node = create_node(i, head); head = new_node; } // Use function to print list print_list(head); // Create an empty list to test print function // Create another list the same way but using insert at tail // Print the list return 0; } struct node *create_node(int data, struct node *next) { struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) { return NULL; } new_node->data = data; new_node->next = next; return new_node; } void print_list(struct node *head) { printf("list:\n"); struct node *curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } void insert_at_tail(struct node *head, int data) { }