链表节点移动至指定索引功能实现遇阻,求代码调试帮助
Fixing Your Linked List Node Reindexing Function
Let's break down the issues in your current code and walk through the corrected version step by step.
Key Problems in Your Current Code
- No handling for missing target node: If no node matches the
nameparameter,nodewill beNULL, and subsequent operations on it will cause a crash. - Failing to remove the target node from its original position: You're only handling the insertion side, but never disconnecting the target node from its previous position. This leads to duplicate nodes or cycles in the linked list.
- Incorrect index traversal logic: Your loop to find the insertion position doesn't account for edge cases (like inserting at the head, or inserting after the last node), and the way you link nodes post-traversal creates invalid pointers.
- No handling for moving the head node: If the target node is the current head, you never update the
*headpointer, which will break the list.
Corrected Function with Explanations
Here's the fixed code, with comments explaining each critical step:
#include <string.h> // Assuming FrameNode is defined as: // typedef struct FrameNode { // struct Frame* frame; // struct FrameNode* next; // } FrameNode; void changeIndex(FrameNode** head, char* name, int index) { // Step 1: Find the target node and its previous node FrameNode* target = *head; FrameNode* prev_target = NULL; int found = 0; while (target != NULL) { if (strcmp(target->frame->name, name) == 0) { found = 1; break; } prev_target = target; target = target->next; } // If target node doesn't exist, exit early if (!found) { return; } // Step 2: Remove the target node from its original position if (prev_target == NULL) { // Target is the head node, update head *head = target->next; } else { // Bypass the target node prev_target->next = target->next; } // Step 3: Find the insertion position (prev_insert is the node before the insertion point) FrameNode* prev_insert = NULL; FrameNode* curr_insert = *head; int current_pos = 1; // Traverse to the insertion index (handle inserting at head or beyond the last node) while (curr_insert != NULL && current_pos < index) { prev_insert = curr_insert; curr_insert = curr_insert->next; current_pos++; } // Step 4: Insert the target node at the new position if (prev_insert == NULL) { // Insert at the head target->next = *head; *head = target; } else { // Insert between prev_insert and curr_insert target->next = curr_insert; prev_insert->next = target; } }
Let's Test This with Your Example
Original list: 1->2->3->4->END
- Target node is
2,prev_targetis1 - Remove
2: list becomes1->3->4->END - Insert at index 4:
prev_insertis4,curr_insertisNULL - Insert
2after4: final list is1->3->4->2->END(matches your expected result)
Additional Edge Cases Handled
- Moving the head node to another index
- Inserting at index 1 (new head)
- Inserting beyond the last node (appending to the end)
- Target node is already at the desired index (you could add a quick check for this to optimize if needed)
内容的提问来源于stack exchange,提问作者prayerShawl
相关产品推荐
相关产品推荐

