如何将链表list1元素按menu分组迁移至链表list2的技术实现问询
Solution
Alright, let's break this down step by step. The core idea is to iterate through every order in list1, group them by their menu number in list2, and make sure the final structure matches the expected output.
First, let's fix a couple of small issues in the base code:
- The
NewOrderfunction is missing a return type—addvoidto it. - The
main()function has a typo:lista2should belist2in theViewAllMenucall. - We need to pass
list2by reference toCreateMenuList()(since we'll modify its head pointer), so the function parameter needs to beNodeM *list2instead ofNodeM list2.
Now, here's the complete implementation of CreateMenuList():
void CreateMenuList(NodeO list1, NodeM *list2) { // Traverse every order in the source list (list1) NodeO current_order = list1; while (current_order != NULL) { int target_menu = current_order->order.menu; NodeM existing_menu = NULL; NodeM current_menu = *list2; NodeM prev_menu = NULL; // Step 1: Check if this menu already exists in list2 while (current_menu != NULL) { if (current_menu->menu.code == target_menu) { existing_menu = current_menu; break; } prev_menu = current_menu; current_menu = current_menu->next; } // Step 2: If menu doesn't exist, create a new menu node if (existing_menu == NULL) { NodeM new_menu = (NodeM)malloc(sizeof(TNodeM)); if (new_menu == NULL) { fprintf(stderr, "Memory allocation failed!\n"); exit(EXIT_FAILURE); } new_menu->menu.code = target_menu; new_menu->menu.orders_list = NULL; new_menu->next = NULL; // Insert the new menu in ascending order (to match expected output: 1, 2, 3) NodeM insert_pos = *list2; NodeM insert_prev = NULL; while (insert_pos != NULL && insert_pos->menu.code < target_menu) { insert_prev = insert_pos; insert_pos = insert_pos->next; } if (insert_prev == NULL) { // Insert at the head of list2 new_menu->next = *list2; *list2 = new_menu; } else { // Insert between insert_prev and insert_pos new_menu->next = insert_pos; insert_prev->next = new_menu; } existing_menu = new_menu; } // Step 3: Add the current order to the menu's order list // Reuse NewOrder() which prepends nodes—this gives us the exact order needed! // For menu 1, list1 has 0000003 first, then 0000004. Prepending makes the list 0000004 -> 0000003, matching the output. NewOrder(current_order->order, &(existing_menu->menu.orders_list)); // Move to the next order in list1 current_order = current_order->next; } }
Don't forget to update the call to CreateMenuList() in main() to pass the address of list2:
CreateMenuList(list1, &list2);
For completeness, here's a simple implementation of ViewAllMenu() that produces the expected output format:
void ViewAllMenu(NodeM list2) { NodeM current_menu = list2; while (current_menu != NULL) { printf("%d ", current_menu->menu.code); NodeO current_order = current_menu->menu.orders_list; while (current_order != NULL) { printf("%s ", current_order->order.code); current_order = current_order->next; } current_menu = current_menu->next; } printf("\n"); }
Key Details to Note:
- Passing
list2by reference: Since we need to modify the head pointer oflist2(when adding the first menu node), we pass a pointer to theNodeMpointer. This ensures changes are reflected in the main function. - Sorted menu order: The expected output shows menus in ascending order (1, 2, 3), so we insert new menu nodes in the correct position to maintain this sorted structure.
- Reusing
NewOrder: The existingNewOrderfunction prepends orders to the list. Since we processlist1from first to last, prepending reverses the order of orders for each menu—exactly what the expected output requires (e.g., menu 1 gets 0000004 first, then 0000003).
内容的提问来源于stack exchange,提问作者St3ve R0nix
相关产品推荐
相关产品推荐

