gpt4 book ai didi

C语言: Error in implementing binary search tree using structures

转载 作者:行者123 更新时间:2023-11-30 19:08:24 25 4
gpt4 key购买 nike

/* These are struct definitions I am using */
struct PdsNdxInfo{
int key;
int offset;
};

struct PdsInfo{
FILE *repo_fptr;
FILE *ndx_fptr;
char repo_name[MAX_NAME_LEN];
int repo_status;
int num_recs;
struct PdsNdxInfo ndxEntries[MAX_RECS];
};


/*This is the code */

//BST Creation
struct PdsNdxInfo temp[pdsInfo.num_recs];
fseek(pdsInfo.ndx_fptr,0,SEEK_SET);
fread(temp, sizeof(struct PdsNdxInfo), pdsInfo.num_recs, pdsInfo.ndx_fptr);


int i=0;
while(i < pdsInfo.num_recs){
printf("********%d %d",temp[i].key,temp[i].offset);
if(root==NULL) {
root =insert(root,temp[i].key,temp[i].offset); //getting error
}
else {
insert(root,temp[i].key,temp[i].offset);
}
i++;
}

/* This is the function definition */
struct node *newNode(int k,int o){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = k;temp->offset = o;
temp->left = temp->right = NULL;
return temp;
}

struct node* insert(struct node* root, int k,int o) {
if (root == NULL) return newNode(k,o);
if (k < root->key)
root->left = insert(root->left, k,o);
else if (k > root->key)
root->right = insert(root->right, k,o);

return root;
}

编译器错误:

pds_version2.c: In function ‘pds_store’:
pds_version2.c:119:9: warning: assignment makes pointer from integer
without a cast [enabled by default]
root = insert(root,pdsInfo.ndxEntries[pdsInfo.num_recs-
1].key,pdsInfo.ndxEntries[pdsInfo.num_recs-1].offset);
^
pds_version2.c: At top level:
pds_version2.c:180:14: error: conflicting types for ‘insert’
struct node* insert(struct node* root, int k,int o)
^
pds_version2.c:66:10: note: previous implicit declaration of ‘insert’
was here
root =insert(root,temp[i].key,temp[i].offset);

无法弄清楚为什么会发生错误,基本上我正在尝试创建二叉搜索树,上面是两个方法 insert 和 newnode 但出现编译时错误。

新节点是一个具有左右两个指针和两个数据值的结构体。但我无法弄清楚为什么会发生这样的错误“赋值使指针来自整数而不进行强制转换[默认启用]”

最佳答案

您应该将插入函数放在使用它的位置之前。或者简单地将函数声明放在前面:

struct node* insert(struct node* root, int k,int o);

void code()
{
//use insert function here
}
struct node* insert(struct node* root, int k,int o)
{
//insert function definition
}

关于C语言: Error in implementing binary search tree using structures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45257061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com