#include<stdio.h>
#include<stdlib.h>
struct bstNode
{
int data;
struct bstNode *left;
struct bstNode *right;
};
struct bstNode* getNewNode(int data)
{
struct bstNode* newNode = (struct bstNode*)malloc(sizeof(struct bstNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
void Insert(struct bstNode** root, int data)
{
if(*root == NULL)
{
*root = getNewNode(data);
}
else
if(data >= (*root)->data)
{
Insert((*root)->right, data);
}
else
{
Insert((*root)->left, data);
}
}
void Search(struct bstNode** root,int data)
{
if(data == (*root)->data)
{
printf("Data Found");
getchar();
exit(0);
}
else
if(data >= (*root)->data)
{
Search((*root)->left, data);
}
else
{
Search((*root)->right, data);
}
}
int main()
{
struct bstNode* root = NULL;
Insert(&root,12);
Insert(&root,13);
Insert(&root,1);
Insert(&root,16);
Insert(&root,8);
Insert(&root,19);
Search(&root,8);
Search(&root,6);
return 0;
}
在上面的代码中,我试图将指针变量的地址从主函数传递给Insert函数。据我所知,这里它应该接受作为指针对指针的论点,但它不接受。问题是什么?请帮忙,这样我也可以更新我的知识。非常感谢。
详细错误如下:
main.cpp:在函数'空插入(bstNode**, int)':main.cpp:32: 37:错误:无法将'bstNode*'转换为'bstNode**'参数'1'到'空插入(bstNode**, int)'插入((*root)-
main.cpp:36: 36:错误:无法将参数'1'的'bstNode*'转换为'bstNode**',以'无效插入(bstNode**, int)'插入(*root)-
main.cpp:56:37:错误:无法将参数“1”的“bstNode*”转换为“bstNode**”到“void Search(bstNode**, int)” 搜索((*根)-
解释
您错过了
但是您发布的代码会导致分段错误,即使在纠正类型错误之后也是如此。这是因为,正如我在评论中提到的,您的程序中存在多个逻辑错误。我在下面列出了它们:
>
您尚未在
Search
函数中指定递归的终止条件。递归搜索最终导致分段错误
。
您在
else if
和else
中递归调用
函数时使用了错误的参数。如果<code>数据
虽然不是错误,但当您在 BST 中找到搜索的值时,您正在使用
exit(0)。
这将立即终止程序,因此,如果 BST 中存在该值,则只能在主函数中使用一次搜索
。
您没有消息指示该值在 BST 中不存在。
我所做的修改包括:
> 搜索
函数中递归
搜索
的终止条件,即检查为 *root == NULL
交换递归
搜索
调用的参数。
添加消息以确定该值何时在 BST 中不存在
添加了用于检查BST中不存在的值的注释搜索
下面是修改后的代码的最终工作版本。我建议您去掉< code>exit(0)
并用其他机制来代替它。
修改的工作代码
#include <stdio.h>
#include <stdlib.h>
struct bstNode
{
int data;
struct bstNode *left;
struct bstNode *right;
};
struct bstNode *getNewNode(int data)
{
struct bstNode *newNode = (struct bstNode *)malloc(sizeof(struct bstNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
void Insert(struct bstNode **root, int data)
{
if (*root == NULL)
{
*root = getNewNode(data);
}
else if (data >= (*root)->data)
{
Insert(&((*root)->right), data);
}
else
{
Insert(&((*root)->left), data);
}
}
void Search(struct bstNode **root, int data)
{
if (*root != NULL)
{
if (data == (*root)->data)
{
printf("Data Found");
getchar();
exit(0);
}
else if (data > (*root)->data)
{
Search(&((*root)->right), data);
}
else
{
Search(&((*root)->left), data);
}
}
}
int main()
{
struct bstNode *root = NULL;
Insert(&root, 12);
Insert(&root, 13);
Insert(&root, 1);
Insert(&root, 16);
Insert(&root, 8);
Insert(&root, 19);
Search(&root, 8);
// Search(&root, 29);
printf("Data Not Found");
return 0;
}
这是实现相同目标的工作解决方案,不使用双重引用 (**
)。
我的解决方案
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *createNode(value){
struct node *newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insert(struct node *root, int data)
{
if (root == NULL) return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
return root;
}
void search(struct node *root, int data, int *found){
if(root == NULL) return;
search(root->left, data, found);
if(root->data == data){
*found = 1;
}
search(root->right, data, found);
}
int main(){
struct node *root = NULL;
root = insert(root, 8);
insert(root, 3);
insert(root, 1);
insert(root, 6);
insert(root, 7);
insert(root, 10);
insert(root, 14);
insert(root, 4);
int found7 = 0, found9 = 0;
search(root, 7, &found7);
search(root, 9, &found9);
found7 ? printf("7 found in BST\n") : printf("7 not found\n");
found9 ? printf("9 found in BST\n") : printf("9 not found\n");
return 0;
}
在像这样的函数的if语句中
if(data >= (*root)->data)
{
Insert((*root)->right, data);
}
else
{
Insert((*root)->left, data);
}
例如表达式(*root)-
Insert( &(*root)->right, data);
我有这个错误 根据代码 -- 例如,当我不通过将其声明为全局来捕获时,它就可以工作。但是当我在我的函数g范围内声明时,会显示此错误。 您知道问题是什么吗?我如何通过将保持在我的函数本地来解决问题?
问题内容: 昨天我参加了一家知名欧洲公司的CTO的演讲,他直到最近才告诉他他不知道Java有指针。在面对他时,他说他对Java中存在指针/不安全代码绝对有把握。 问题答案: 有一个叫做的类,这是真的。但是它没有在Java代码中使用指针(因为Java没有指针,尽管我同意Java引用在概念上是相似的),所以大多数都是使用本机代码实现的。 正如我在评论中提到的那样,这不是公共API的一部分,不应由客户端
本文向大家介绍C语言中的指针以及二级指针代码详解,包括了C语言中的指针以及二级指针代码详解的使用技巧和注意事项,需要的朋友参考一下 很多初学者都对C中的指针很迷糊,希望这篇blog能帮助到大家: 1.什么是“指针”: 在执行C程序的时候,由于我们的数据是存储在内存中的。所以对于C程序本身来说,如果想找到相应被调用的数据,就要知道存储该数据的内存地址是多少,换言之,C程序通过已知的内存地址到相应的内
1. 指针的基本概念 在第 12 章 栈与队列讲过,堆栈有栈顶指针,队列有头指针和尾指针,这些概念中的“指针”本质上是一个整数,是数组的索引,通过指针访问数组中的某个元素。在图 20.3 “间接寻址”我们又看到另外一种指针的概念,把一个变量所在的内存单元的地址保存在另外一个内存单元中,保存地址的这个内存单元称为指针,通过指针和间接寻址访问变量,这种指针在C语言中可以用一个指针类型的变量表示,例如某
我最近将firebase数据库的订单更改为Location,我知道我漏了一些东西,但我找不到确切的问题。有人能指出我在这里做错了什么吗?