【发布时间】:2019-10-15 12:35:13
【问题描述】:
我正在尝试从双向链表中删除一个节点,但我遇到了问题 这个问题是当节点是第一个或中间它打印 0 而不是真正删除它,但是当它是列表中的最后一个节点时它工作得很好,这里是代码:
dList* del(dList*ptr, int x)
{
dList *itr = NULL;
for( itr = ptr; itr != NULL; itr = itr -> next)
{
// if the element is the first in the list
if(itr -> value == x && itr -> prev == NULL)
{
itr -> next -> prev = NULL;
ptr = itr -> next;
free(itr);
}
// if the element is the last in the list
else if(itr -> value == x && itr -> next == NULL)
{
itr -> prev -> next = NULL;
free(itr);
}
// if its in the middle
else if(itr -> value == x){
(itr -> prev) -> next = itr -> next;
(itr -> next) -> prev = itr -> prev;
free(itr);
}
}
return ptr;
}
提前致谢!
【问题讨论】:
-
是的,我的意思是,我会在 atm 编辑它
-
我会尽力解决这个问题,非常感谢!
标签: c data-structures struct linked-list doubly-linked-list