gpt4 book ai didi

c - 从 C 中的队列前面删除值

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:18 26 4
gpt4 key购买 nike

我正在尝试使用此函数从队列的前面删除一个值。它似乎第一次工作(尽管我对此可能是错误的)但是当第二次释放节点时它会触发一个使程序崩溃的断点。我该如何解决这个问题?

printf("Value removed = %d\n", dequeue(myQueue));

int dequeue(queue q)
{
if (q == NULL || q->head == NULL)
{
return 0;
}

node * head = q->head;
node * temp = head;

if (temp == NULL)
{
return;
}

int returnValue = head->value;

//Free first node in queue
head = temp->next;
free(temp);

//Return value that was removed
return returnValue;
}

最佳答案

你更新一个局部变量,而不是更新队列结构 q 中的 head:

head = temp->next;  // this is local variable 

应该是:

q->head = temp->next;

关于c - 从 C 中的队列前面删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21601427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com