//This program introduced the linked list data structure //We are able to insert at the end of a linked list with //create_node function and are able to traverse the list //with the print function #include #include //Define a node struct node { int data; struct node *next; }; //Helper functions //TODO: Fill in the output and inputs to complete prototypes struct node *create_node(int data, struct node *next); void print_list(struct node *head); int main (void) { //TODO: Create a pointer to the first node of the list, and the first node //The first node is pointing to NULL as the next node, since there //are no other nodes struct node *head = create_node(4, NULL); //TODO: Create the next node, with data 2 and pointing next to the current head //Therefore you are making this new node the head of the list head = create_node(2, head); //TODO: Create the next node, with data 1 and pointing next to the current head //Therefore you are making this new node the head of the list head = create_node(1, head); //TODO: What are we giving our print_list function? print_list(head); return 0; } //Function that creates a node // TODO: Fill in input and output to complete function struct node *create_node(int data, struct node *next) { //TODO: create a pointer to a node of type struct node //by using malloc to allocate memory //(4, NULL) // new_node = some address that points to that node //(2, head) struct node *new_node = malloc(sizeof(struct node)); //TODO: Initialise the data by setting it to the given int //new_node (int data) = 4 //new_node (int data) = 2 new_node->data = data; //TODO: Initialise next pointer to point to the next node given //new_node (struct node *next) = NULL // new_node(struct node *next) = head (4) new_node->next = next; //TODO: What will I need to return? return new_node; } //Function that prints a list // TODO: Fill in input and output to complete function void print_list(struct node *head) { //TODO: create a way to keep track of where we are in the list struct node *current = head; //TODO: What will be the condition where our while loop can stop while (current != NULL) { //TODO: print out the node value printf("%d -> ", current->data); //TODO: increment to the next node current = current->next; } //Finish printing list with an X printf("X\n"); }