我已经搜索了大约2个小时,我找不到原因,但是在1个特定位置,我的代码显然将“ next”节点设置为Null,并且优先级队列的其余部分被清除了。也许我忽略了某些内容,但是我已经搜索了我的代码,但似乎找不到问题。功能如下:
adoptedCallback
这是我用来测试代码的输入:
void PriorityQueue_add(PriorityQueue *pq, Node *newNode){
printf("New Node:");
node_print(newNode);
newNode->in_queue = TRUE;
if(pq->head == NULL){
pq->head = newNode;
}else if(newNode->lowest_cost <= pq->head->lowest_cost){
newNode->next = pq->head;
pq->head = newNode;
}else if(pq->head->next == NULL){
pq->head->next = newNode;
}else{
Node *prevNode = calloc(1, sizeof(Node));
Node *tempNode = malloc(sizeof(Node));
tempNode = pq->head;
printf("TempNode next: %d\n", tempNode->next->id);
while(tempNode != NULL && (newNode->lowest_cost > tempNode->lowest_cost)){
printf("RUNS\n");
prevNode = tempNode;
tempNode = tempNode->next;
printf("PrevNode: %d\n", prevNode->id);
printf("TempNode: %d\n", tempNode->id);
}
if(tempNode = NULL){
printf("RUNNING 1\n");
prevNode->next = newNode;
}else{
printf("RUNNING 2\n");
prevNode->next = newNode;
newNode->next = tempNode;
}
}
}
此外,输出如下:
////=============== NODE TEST =================
Node *test1 = malloc(sizeof(Node));
Node *test2 = malloc(sizeof(Node));
Node *test3 = malloc(sizeof(Node));
Node *test4 = malloc(sizeof(Node));
Node *test5 = malloc(sizeof(Node));
node_init(test1);
node_init(test2);
node_init(test3);
node_init(test4);
node_init(test5);
test1->id = 4;
test2->id = 7;
test3->id = 9;
test4->id = 13;
test5->id = 17;
test1->lowest_cost = 5;
test2->lowest_cost = 8;
test3->lowest_cost = 10;
test4->lowest_cost = 14;
test5->lowest_cost = 18;
//============ Priority Queue TEST =============
PriorityQueue *pq = malloc(sizeof(PriorityQueue));
PriorityQueue_init(pq);
PriorityQueue_add(pq, test3);
PriorityQueue_print(pq);
PriorityQueue_add(pq, test4);
PriorityQueue_print(pq);
PriorityQueue_add(pq, test1);
PriorityQueue_print(pq);
PriorityQueue_add(pq, test2);
PriorityQueue_print(pq);
PriorityQueue_add(pq, test5);
PriorityQueue_print(pq);
我知道最后会出现段错误,因为当tempNode为Null时我试图打印tempNode-> id,但我对此并不在意。我对为什么在第四次插入时列表从ID [4,9,13]重新启动为[4,7]感到困惑。预先感谢。
==================================编辑============ ======================
我每个人都发现了问题。它位于else语句内的if语句中。我不小心有1个等号,而不是2个……很简单,但我却忽略了它。