【发布时间】:2020-08-12 22:25:50
【问题描述】:
这里面有什么我不明白的地方吗,或者这是我需要使用 alloc 函数的时候之一。 >stdlib.h 我以前从未使用过。
struct node {
int value;
node* left;
node* right;
node* parent;
};
这是导致程序崩溃的代码。
// Is the value > current node?
// If it is, then go right,
// If not then go left.
// Is there a node forward?
// If not then create a node with the value.
node currentNode = rootNode;
bool newNodeCreated = false;
while (newNodeCreated == false){
if (value > currentNode.value){ // If the value is greater than the currents node.
if (currentNode.right != NULL){ // Check the child on the right.
currentNode = *currentNode.right; // Set the current node to the child and restart the loop.
} else {
currentNode.right->value = value; // Set a new node.
currentNode.right->parent = ¤tNode;
newNodeCreated = true; // End loop.
}
} else { // If the value is less than the current node.
if (currentNode.left != NULL){ // Check the child on the left.
currentNode = *currentNode.left; // Set the current node to the child and restart the loop.
} else {
currentNode.left->value = value; // Set a new node.
currentNode.left->parent = ¤tNode;
newNodeCreated = true; // End loop.
}
}
}
return;
【问题讨论】:
-
它可能不会“冻结”,它只是进入无限循环,除非您得到特定的崩溃结果。你有没有把它放到调试器中看看它卡在哪里?
-
我试过了,但它还是冻结了。我把断点放在循环的顶部。
-
当你说“冻结”是什么意思?代码不会“冻结”,计算机会。
-
您在问题中使用了“崩溃”这个词,它是死机还是崩溃?
-
它停止工作并在我的 IDE 窗口后面。没有错误。
标签: c pointers binary-search-tree