【发布时间】:2017-01-14 22:38:29
【问题描述】:
我正在使用 C 编写一个简单的 AVL 树实现。我的代码在各个部分都有问题。有时我会收到此错误,有时取消引用效果很好。
这是我的 struct 节点 的样子:
struct Node
{
int data;
struct Node *left;
struct Node *right;
int height;
};
这是我得到解引用错误的地方(正好在 if (data data)))
struct node* search(struct node* p, int data)
{
if (!p)
return NULL;
if (data < (p->data))
return search(p -> left, data);
else if ( data > p -> data )
return search(p -> right, data);
else
return p;
}
也在这里:
struct Node remove_min(struct Node *x)
{
if (x->left == NULL)
return x->right;
x->left = deleteMin(x->left);
return x;
}
任何帮助将不胜感激。谢谢
【问题讨论】:
-
struct Node定义和您在同一文件中看到此错误的代码是否相同?如果不是,您能否更明确地说明您的代码是如何组织的,您的#include语句是什么样的等等?理想情况下,您可以提供一个简单的复制器来完全展示问题。 -
您的代码在哪里,当您调用搜索时,我的意思是您的主要代码?
-
我已经发布了我的整个代码作为答案,你可以在那里看看,谢谢。
-
struct Node {....} node;声明了一个名为node类型为struct Node的变量。没有struct node,因此您的错误。
标签: c pointers linked-list dereference avl-tree