【发布时间】:2020-09-04 18:29:36
【问题描述】:
我正在尝试编写一个函数,该函数正在从我的链接列表中删除一个特定元素,但是当我到达该元素时它会因分段错误而崩溃。 这是我的代码的一部分
typedef struct dlist_t {
int data;
struct dlist_t *prev, *next;
} dlist_t;
typedef struct list_t {
dlist_t *head, *tail;
} list_t;
int delElement(list_t *list, int elem) {
while (list) {
if ((list->head)->data == elem) {
list->head->next = list->head->prev;
list->head->prev = list->head->next;
free(list);
return 1;
}
list = list->head->next;
}
return 0;
}
【问题讨论】:
-
看来代码没有意义。显示列表定义。
-
用图表自己画出来。顺便说一句,这是不完整的代码。我们不知道结构是什么样子(尽管我们可能有一个好主意),我们不知道您如何分配您在此处释放的内存以及您如何填充链接列表。而且您可能对结构进行了类型定义(您不应该这样做,因为它没有任何帮助)。
标签: c struct linked-list doubly-linked-list function-definition