【发布时间】:2015-08-10 10:20:01
【问题描述】:
我浏览了一些解释双向链表中节点删除的文章,但我无法理解为什么以下代码不起作用。请提出一些解决方案。
我有两个结构 A 和 B。有一个结构 A 的链表,每个结构都包含 B 的双向链表。我试图从每个 A 中删除所有 Id 小于值的 B 结构.这是我正在尝试的方法。
typedef struct __B {
int id;
struct __B *next;
struct __B *prev;
} B;
typedef struct __A {
B *bList;
struct __A *next;
} A;
void DeleteNodes(int value, A* AList) {
while(AList != NULL) {
B *BList = AList->bList;
while(BList != NULL) {
B *temp = BList;
BList = BList->next;
if(temp->id < value) {
if(temp->prev == NULL) // delete first node
BList->prev = NULL;
else {
temp->prev->next = BList;
temp->next->prev = temp->prev;
}
temp->next = NULL;
temp->prev = NULL;
free(temp);
temp = NULL;
}
}
AList = AList->next;
}
}
但是当我遍历 AList 和相应的 BLists 时,明显删除的节点仍然存在,这导致应用程序崩溃。 请分享一些建议。
【问题讨论】:
-
OT:根据 C11Draft/7.1.3,
__B的定义是不允许的:“所有以下划线开头的标识符,无论是大写字母还是另一个下划线,始终保留给任何用途。" (iso-9899.info/n1570.html#7.1.3)
标签: c pointers linked-list free