如何解决具有两个结构之间的指针的二进制搜索树帮助
| 我有一个即将完成的家庭作业,但是我一直呆在某个地方。我必须警告,这是我第一次使用指针和所有这些奇怪的东西,所以我很迷路。我的目的是从txt学生数据列表中读取(姓氏ID)。诀窍是我必须使用一个二进制搜索树来存储姓氏(我已经这样做了),并在第一棵树中创建另一个二进制搜索树,该树中存储了学生的名字和ID(部分完成)。问题是,当某些学生具有相同的姓氏和不同的姓氏时,我不能为姓氏创建新节点,但必须将新学生的姓氏和ID放入现有的姓氏节点中。应该是这样的: 卡梅伦·詹姆斯12131313 安德鲁17286378(他的姓氏也是卡梅伦) 代码是:typedef struct nameANDid{
char first[20];
int ID;
struct node *nleft;
struct node *nright;
}yohoho;
typedef struct node{
char last[20];
struct nameANDid yohoho;
struct node *left;
struct node *right;
}node;
///
struct node temp;
struct nameANDid temp2;
struct node *top=NULL;
struct nameANDid *topname=NULL;
void loadData();
struct nameANDid * add_node_nameANDid(struct nameANDid *,struct nameANDid *);
/////
struct node * add_node (struct node *,struct node *);
struct node * search_node (struct node *,char *);
void print_node (struct node *);
void print_tree (struct node *);
我主要调用loadData()导入学生
loadData(&temp);
并且loadData()是
void loadData(struct node *temp){
int i;
FILE *fp;
fp=fopen(FILENAME,\"r\");
if (fp == NULL) printf(\"File does not exist\\n\");
for (i=0; i<20; i++){
fscanf(fp,\"%s\",&temp->last);
fscanf(fp,&temp->yohoho.first);
fscanf(fp,\"%d\",&temp->yohoho.ID);
top=add_node(top,temp);
}
fclose(fp);
printf(\"\\n\\nFile loaded\\n\");
}
我调用add_node()在我的主(姓)树中插入一个新节点。这个还可以
struct node * add_node (struct node *top,struct node *temp){
struct node *newNode;
if (top == NULL){
newNode=(struct node *)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
if (memcpy(newNode,temp,sizeof(struct node)) == NULL) {
printf(\"Node addition failed\\n\");
return NULL;}
else {
//printf(\"Node added\\n\");
return newNode;}
}
else {
if (stricmp(temp->last,top->last) < 0){
// printf(\"left\\n\");
top->left=add_node(top->left,temp);}
else if (stricmp(temp->last,top->last) == 0){
// printf(\"Last names are equal\\n\");
topname=add_node_nameANDid(topname,temp2);} //Here is one of my problems
else {
// printf(\"right\\n\");
top->right=add_node(top->right,temp);}
// printf(\"Node added\\n\");
return top;
}
return NULL;
}
我的问题始于(topname = add_node_nameANDid(topname,temp2);),它是一个类似于add_node()的函数,但是如果学生的姓氏相同,她会添加新的nameANDid节点。.我不知道要使用什么参数。 。我讨厌指针,因为我没有使用过指针的经验(至少不湿)。
并且add_node_nameANDid()是
struct nameANDid * add_node_nameANDid (struct nameANDid *topname,struct nameANDid *temp){
struct nameANDid *newNode_nameANDid;
if (topname == NULL){
newNode_nameANDid=(struct nameANDid *)malloc(sizeof(struct nameANDid));
temp->nleft=NULL;
temp->nright=NULL;
if (memcpy(newNode_nameANDid,sizeof(struct nameANDid)) == NULL){
printf(\"Node addition failed\\n\");
return NULL;}
else {
//printf(\"Node added\\n\");
return newNode_nameANDid;}
}
else {
if (stricmp(temp->first,topname->first) <= 0){
// printf(\"leftname\\n\");
topname->nleft=add_node_nameANDid(topname->nleft,temp);}
else {
// printf(\"rightname\\n\");
topname->nright=add_node_nameANDid(topname->nright,temp);}
// printf(\"Node added\\n\");
return topname;
}
return NULL;
}
在add_node_nameANDid()中,我尝试使用类似的变量以使其更易于理解。
我应该如何在add_node_nameANDid()中使用指针,因为当我向它提交时,它说
[警告]在行中从不兼容的指针类型传递'add_node_nameANDid \'的arg 1
topname->nleft=add_node_nameANDid(topname->nleft,temp);}(in add_node_nameANDid())
或“ add_node_nameANDid \”的参数2的类型不兼容
topname=add_node_nameANDid(topname,temp2);}
当我从add_node()调用add_node_nameANDid()时。
有人可以帮我解决这个问题吗?
解决方法
看来问题在于您对左右两个结构都使用了“ 7”。所以发生的事情是您正在将
struct nameANDid
复制到struct node
中。我建议在nameANDid
中需要nleft
和nright
来指向struct nameANDid
,而不是struct node
。
编辑:还有其他各种问题,例如,我认为其目的是研究struct节点中的yohoho
以获取名字的二叉树。 add_node_nameANDid
也将temp->nleft
和temp->nright
设置为null
,不确定这是正确的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。