读书人

构造体指针赋值为NULL, 不被执行

发布时间: 2013-10-02 13:10:38 作者: rapoo

结构体指针赋值为NULL, 不被执行
最近用c实现基本数据结构,用的codeblocks+gcc。二叉树这里出了点问题,请高手指点一下:

实现的是基本的二叉树操作:初始化,左插入,又插入,左删除,右删除。。。 用了一个树测试了一下,发现输出完左子树,该输出右子树的时候就出现segmentation fault. 然后单步跟踪了一下,发现初始化函数initiate(BiTreeNode *root)执行的时候有一条语句总是好像没被执行一样:root->leftChild = NULL;
单步执行时root->leftChild总是一个非NULL的值, root->leftChild->data总是'/', 不知道问题出在哪里

下面是 main.c


typedef char DataType;
#include <stdio.h>
#include <stdlib.h>
#include "BiTree.h"

int main()
{
BiTreeNode *root, *p;

initiate(root);
p = insertLeftNode(root, 'A');
p = insertLeftNode(p, 'B');
p = insertLeftNode(p, 'D');
p = insertRightNode(p, 'G');
p = insertRightNode(root->leftChild, 'C');
insertLeftNode(p, 'E');
insertRightNode(p,'F');
preOrder(root);
return 0;
}


下面是BiTree.h

typedef struct node {
DataType data;
struct node *leftChild;
struct node *rightChild;
} BiTreeNode;

void initiate(BiTreeNode *root) {
root = (BiTreeNode *)malloc(sizeof(BiTreeNode));
root->leftChild = NULL;
root->rightChild = NULL;
}

//if current node is not null, insert a new node to the left node of curr and
//return a pointer of that node
BiTreeNode *insertLeftNode(BiTreeNode *curr, DataType x) {
BiTreeNode *s, *t;

if(curr == NULL)
return NULL;
t = curr->leftChild;
s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data = x;
s->rightChild = NULL;
curr->leftChild = s;
s->leftChild = t;
return s;
}

//if current node is not null, insert a new node to the right node of curr and
//return a pointer of that node
BiTreeNode *insertRightNode(BiTreeNode *curr, DataType x) {
BiTreeNode *s, *t;

if(curr == NULL)
return NULL;
t = curr->rightChild;
s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data = x;
s->leftChild = NULL;
curr->rightChild = s;
s->rightChild = t;
return s;
}

//destroy
void destroy(BiTreeNode *root) {
if(root != NULL && root->leftChild != NULL)
destroy(root->leftChild);
if(root != NULL && root->rightChild != NULL)
destroy(root->rightChild);
free(root);
}

//delete left tree
BiTreeNode *deleteLeftTree(BiTreeNode *curr) {
if(curr == NULL && curr->leftChild == NULL)
return NULL;
destroy(curr->leftChild);
curr->leftChild == NULL;
return curr;


}

//delete right tree
BiTreeNode *deleteRightTree(BiTreeNode *curr) {
if(curr == NULL && curr->rightChild == NULL)
return NULL;
destroy(curr->rightChild);
curr->rightChild == NULL;
return curr;
}

//visit an element in the tree, you can change it later
void visit(DataType d) {
printf("visit: %c\n", d);
}

//traverse the tree in preorder
void preOrder(BiTreeNode *root) {
if(root != NULL) {
visit(root->data);
preOrder(root->leftChild);
preOrder(root->rightChild);
}
}

结构体,二叉树,NULL指针
[解决办法]
正解如下:
int main(){
BiTreeNode *root, *p;
initiate(&root);
..
}
void initiate(BiTreeNode **root) {
*root = (BiTreeNode *)malloc(sizeof(BiTreeNode));
..
}
指针使用形式问题,有点绕。
[解决办法]
http://www.cppblog.com/liujiajia/archive/2008/07/14/56135.html
[解决办法]
完善一下:

template <typename T>class ref_t
{
union{
T *ptr;
const T *cptr;
};
public:
ref_t(const T& t):cptr(&t){};
ref_t(T&t):ptr(&t){};

ref_t &operator =(const ref_t& t)
//没有办法因为参数传递,要用这个,参数传递时,引用隐藏的指针,需要值传递,但也许这就是真相。
{
cptr =t.cptr;
return *this;
}
ref_t &operator =(ref_t& t)//引用的赋值是用值赋值,不是改变引用的对象。
{
*ptr = *t.ptr;
return *this;
}
ref_t &operator =(const T& t){

*ptr= t;
return *this;};

operator T(){return *ptr;} //引用的值
operator const T()const {return *cptr;} //

T *operator &(){return ptr;}; //引用的地址
const T*operator&()const {return cptr;};


};

template <typename T> ref_t<T> ref_of(T &x) {return ref_t<T>(x);}


template <typename T> void swap_ref_t(ref_t<T> a,ref_t<T> &b)
{
//const T t= a;

T t= a;
a =b;
b =t;
}
#define ref_swap(x,y) {swap_ref_t(ref_of(x),ref_of(y));}

int main()
{
int d=10;
ref_t<int> r(d);
int d2=20;

cout<<"var d: value="<<d<<",adderss ="<<&d<<endl;
cout<<"ref r: value ="<<r<<",address ="<<&r <<endl;
r=d2;
cout<<"ref r: value ="<<r<<",address ="<<&r <<endl;

cout<<r<<endl;

ref_t<int> r2(d2);
r= r2;
cout<<"ref r: value ="<<r<<",address ="<<&r <<endl;
cout<<"ref r2: value ="<<r2<<",address ="<<&r2 <<endl;



int x=5,y=6;
cout<<"x="<<x<<",y= "<<y<<endl;
ref_swap(x,y);
cout<<"x="<<x<<",y= "<<y<<endl;
ref_swap(x,y);

cin.get();
}

读书人网 >C语言

热点推荐