// Pantea Aria // linked list // - create_node // - print_list // - insert_at_tail // - delete_first // - delete_all // - search_and_delete #include #include struct node { int data; struct node *next; }; struct node *create_node(int data, struct node *next); void print_list(struct node *head); struct node *insert_at_tail(struct node *head, int data); struct node *delete_first(struct node *head); struct node * delete_all(struct node *head); struct node *search_and_delete(struct node *head, int value); int main(void) { struct node *head = NULL; // Print the empty list print_list(head); printf("Search and delete from empty list\n"); // Try deleting 99 from an empty list head = search_and_delete(head, 1000); print_list(head); // Build a list that stores 0..9 by inserting at the tail for (int i = 0; i < 10; i++) { head = insert_at_tail(head, i); } print_list(head); printf("delete first '0'\n"); // Delete first head = delete_first(head); print_list(head); //delete_all(head); // print_list(head); printf("Search and delete 4\n"); // Delete 4 (middle case) head = search_and_delete(head, 4); print_list(head); printf("Search and delete 1000\n"); // Delete 99 (not found case) head = search_and_delete(head, 1000); print_list(head); printf("Search and delete 9\n"); // Delete 9 (tail case) head = search_and_delete(head, 9); print_list(head); printf("Search and delete 0\n"); // Delete 0 (head case) head = search_and_delete(head, 0); print_list(head); // Free all remaining nodes to prevent memory leaks head = delete_all(head); print_list(head); return 0; } // TODO: Allocate memory for a new node using malloc. // TODO: If malloc fails, return NULL. // TODO: Set new_node->data and new_node->next, then return new_node. struct node *create_node(int data, struct node *next) { // TODO } // TODO: Traverse from head to NULL and print each node’s data. // TODO: Print something like: "list:" then the values on one line. void print_list(struct node *head) { // TODO } // TODO: Insert a new node (with given data) at the end of the list. // Cases to handle: // - Empty list: new node becomes the head. // - Non-empty list: traverse to last node, then link it to new node. // Return the (possibly updated) head. struct node *insert_at_tail(struct node *head, int data) { // TODO } // TODO: Delete the first node (if it exists) and free it. // Return the new head pointer. struct node *delete_first(struct node *head) { // TODO } // TODO: Free every node in the list. // Hint: you must save the next pointer before calling free on current. // issues here struct node *delete_all(struct node *head) { // TODO } // TODO: Find the first node whose data == value, remove it from the list, // and free it. // Cases to handle: // - Empty list // - Value not found // - Deleting head node // - Deleting middle/tail node // Return the updated head. struct node *search_and_delete(struct node *head, int value) { }