从链表中删除节点问题 C
Deleting node from linked list issues C
我只是想从链表中删除一个节点,但似乎遇到了问题。我想知道是否有人可以看看可能出了什么问题?谢谢!
struct ets {
struct node *equip_head;
struct node *member_head;
struct node *loan_head;
const char *equip_fname;
const char *member_fname;
const char *loan_fname;
};
struct node {
void *data; /* Accepts all data, yay */
struct node *next;
};
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *tmpNode = current;
struct member_data *member_data = NULL;
while (current != NULL) {
member_data = current->data;
if (strcmp(member_data->MemberID, MemberID) == 0) {
tmpNode = current;
current = current->next;
free(tmpNode->data);
free(tmpNode);
return TRUE;
}
current = current->next;
}
return FALSE;
}
您没有从列表中删除节点。您可以这样做以从列表中删除节点:
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *prev=NULL;
struct member_data *member_data = NULL;
while(current != NULL) {
member_data = current->data;
if(strcmp(member_data->MemberID, MemberID) == 0) {
if(prev==NULL) // removing 1st node
ets->member_head=current->next;
else
prev->next=current->next; // removing current node from list
free(current->data);
free(current);
return TRUE;
}
prev = current;
current = current->next;
}
return FALSE;
}
因为你有一个链表,所以删除算法被破坏了。这是您目前正在做的事情:
- 找到要删除的成员。美好的。你有
node-1 -> node -> node+1
- 您删除该成员。为什么不。但是你的列表会变成
node-1 -> unallocated node
并且无法找到下一个节点
您应该改为测试下一个节点是否具有正确的 ID 以具有类似的内容:prev_node(current)
-> node_to_delete -> next_node`
那么你可以这样做:
tmpNode = current->next;
current->next = tmpNode->next; /* ok for the chaining */
free(tmpNode->data); /* deletion will be ok */
free(tmpNode);
当然对第一个和最后一个节点进行特殊管理...
编辑:阿里已经给出了答案。我留下这个作为评论为什么 OP 的算法被破坏
我只是想从链表中删除一个节点,但似乎遇到了问题。我想知道是否有人可以看看可能出了什么问题?谢谢!
struct ets {
struct node *equip_head;
struct node *member_head;
struct node *loan_head;
const char *equip_fname;
const char *member_fname;
const char *loan_fname;
};
struct node {
void *data; /* Accepts all data, yay */
struct node *next;
};
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *tmpNode = current;
struct member_data *member_data = NULL;
while (current != NULL) {
member_data = current->data;
if (strcmp(member_data->MemberID, MemberID) == 0) {
tmpNode = current;
current = current->next;
free(tmpNode->data);
free(tmpNode);
return TRUE;
}
current = current->next;
}
return FALSE;
}
您没有从列表中删除节点。您可以这样做以从列表中删除节点:
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *prev=NULL;
struct member_data *member_data = NULL;
while(current != NULL) {
member_data = current->data;
if(strcmp(member_data->MemberID, MemberID) == 0) {
if(prev==NULL) // removing 1st node
ets->member_head=current->next;
else
prev->next=current->next; // removing current node from list
free(current->data);
free(current);
return TRUE;
}
prev = current;
current = current->next;
}
return FALSE;
}
因为你有一个链表,所以删除算法被破坏了。这是您目前正在做的事情:
- 找到要删除的成员。美好的。你有
node-1 -> node -> node+1
- 您删除该成员。为什么不。但是你的列表会变成
node-1 -> unallocated node
并且无法找到下一个节点
您应该改为测试下一个节点是否具有正确的 ID 以具有类似的内容:prev_node(current)
-> node_to_delete -> next_node`
那么你可以这样做:
tmpNode = current->next;
current->next = tmpNode->next; /* ok for the chaining */
free(tmpNode->data); /* deletion will be ok */
free(tmpNode);
当然对第一个和最后一个节点进行特殊管理...
编辑:阿里已经给出了答案。我留下这个作为评论为什么 OP 的算法被破坏