我只是在树中练习一下,并且无法弄清楚为什么当我试图在我的二叉查找树中插入一个vcard时,我总是得到一个seg故障......任何帮助?谢谢。 我附加了函数,以及一些我用来运行函数的测试。 出于某种原因,它适用于一种情况(当我与bst匹配ID2时),但在尝试将ID1与bst匹配时不起作用。打印答案时出现分段错误
谢谢!
typedef struct {
char *cnet;
char *email;
char *fname;
char *lname;
char *tel;
} vcard;
typedef struct bst bst;
struct bst {
vcard *c;
bst *lsub;
bst *rsub;
};
int bst_insert(bst *t, vcard *c) {
if (t->c == NULL) {
fprintf (stderr, "Empty vcard");
}
if (strcmp(c->cnet,t->c->cnet) == 0) {
return 0;
}
else if ((strcmp(c->cnet,t->c->cnet) < 0) && (t->lsub == NULL)) {
t->lsub->c = c;
return 1;
}
else if ((strcmp(c->cnet, t->c->cnet) < 0) && (t->lsub != NULL)) {
bst_insert(t->lsub, c);
}
else if ((strcmp(c->cnet, t->c->cnet) > 0) && (t->rsub == NULL)) {
t->rsub->c = c;
return 1;
}
else if ((strcmp(c->cnet, t->c->cnet) > 0) && (t->rsub != NULL)) {
bst_insert(t->rsub, c);
}
}
int main() {
vcard NewID;
NewID.cnet = "leeholim";
NewID.email = "[email protected]";
NewID.fname = "leeho";
NewID.lname = "lim";
NewID.tel = "555-555-5555";
vcard NewID2;
NewID2.cnet = "donalfonsodailey";
NewID2.email = "[email protected]";
NewID2.fname = "My name's donny";
NewID2.lname = "dailey";
NewID2.tel = "212-323-1234";
bst ID;
ID.c = &NewID;
ID.lsub = NULL;
ID.rsub = NULL;
printf("%s\n", Poop->c->fname);
printf("%d\n", bst_insert(&ID, &NewID2));
}
发布足够的代码,其他人可以编译并运行它并查看相同的结果。 [see here](http://stackoverflow.com/help/mcve)获取更多信息 – 2015-02-11 03:29:28
那么你去了,我加了“int main(){}” – 2015-02-11 03:32:45
'bst_singleton()'在哪里? – timrau 2015-02-11 03:33:04