// Pantea Aria // 1) How could I print the data in the first node? // 2) How could I print the data in the second node? // 3) How could I modify the next field in the first node to be NULL? What would that do? #include #include struct node { int data; struct node *next; }; struct node *create_node(int data, struct node *next); int main(void) { // Build a simple list with 3 nodes: 10 -> 20 -> 30 -> NULL struct node *third = create_node(30, NULL); struct node *second = create_node(20, third); struct node *head = create_node(10, second); printf("List is: 10 -> 20 -> 30 -> NULL\n"); // TODO 1: Print the data in the first node (head). // Hint: the first node is head itself. printf("First node data: %d\n", head->data); // TODO 2: Print the data in the second node. // Hint: the second node is the node after head. printf("Second node data: %d\n", head->next->data); // TODO 3: Set the next field in the first node to NULL. // What would that do? // Hint: it "cuts" the list after the first node. // head->next = NULL; // After TODO 3, try printing what the head points to now: printf("\nAfter setting head->next to NULL:\n"); printf("Head data: %d\n", head->data); // TODO 4 (optional): Check if there is still a second node reachable from head. // If you cut the list correctly, head->next should be NULL. if (head->next != NULL) { printf("There is still a second node reachable!\n"); } else { printf("No second node reachable from head (list was cut).\n"); } // Cleanup: // IMPORTANT: If you cut the list, you *still* need to free the other nodes // using the pointers you saved (second, third), otherwise you leak memory. free(head); free(second); free(third); return 0; } struct node *create_node(int data, struct node *next) { struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) { printf("Out of memory\n"); exit(1); } new_node->data = data; new_node->next = next; return new_node; }