问题描述
struct someEvent
{
int id;
struct someEvent *prev;
struct someEvent *next;
} * someEvent;
struct someEvent *someEventQueue = NULL;
int main(){
//There is some conditions. If passing all of conditions then It is inserted to someEventQueue.
struct someEvent **curr = &someEventQueue;
if ((*curr) != NULL)
{
while ((*curr) != NULL)
{
//After some processes,I want to delete (*curr)
if ((*curr)->prev == NULL && (*curr)->next == NULL)
{
//I don't kNow how to do this........................
}
if ((*curr)->prev != NULL)
{
(*curr)->next->prev = (*curr)->prev;
}
if ((*curr)->next != NULL)
{
(*curr)->prev->next = (*curr)->next;
}
curr = &(*curr)->next;
}
}
}
我想删除someEventQueue中显示的节点。 someEventQueue是双向链接列表。我试图制作一个删除节点代码。但是,我失败了。我不知道为什么,但它返回细分错误。如何解决这个问题?
解决方法
要简单地删除节点,您可以执行以下操作:
// dnode is the node to delete
if ((*dnode)->prev != NULL) { // checking if this is the first
(*dnode)->prev->next = (*dnode)->next;
if ((*dnode)->next != NULL) { // checking if this is the last
(*dnode)->next->prev = (*dnode)->prev;
}
} else { // here the node is the first
(*dnode) = (*dnode)->next;
if ((*dnode) != NULL) { // checking if he was the last
(*dnode)->prev = NULL;
}
}
现在您的节点已从链接列表中删除,但请注意列表中 光标 的位置,并释放dnode
。