【问题标题】:Binary search tree and structure二叉搜索树和结构
【发布时间】:2021-02-07 11:11:44
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* left;
    struct node* right;
};


struct node(*new_node)(int);
struct node(*insert)(struct node*,int);
int search(struct node*, int);


int main()
{
    struct node* root;
    while(1)
    {
        int choice,data;
        printf("Enter Operation Choice To Perform\n1.Insert\n3.Search\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: printf("Enter data");
                    scanf("%d",&data);
                    root=insert(root, data);
                    break;
            case 3: printf("Enter Data To Search\n");
                    scanf("%d",&data);
                    search(root,data);
                    break;
            case 5: exit(0);
                    break;
            default:printf("INVALID INPUT");
        }
    }

}
struct node(*new_node)(int data)
{
    struct node* new_node=(struct node*)malloc(sizeof(struct node));
    new_node->data=data;
    new_node->left=NULL;
    new_node->right=NULL;
    return new_node;
}
struct node (*insert)(struct node* root,int data)
{
    if(root==NULL)
        root=new_node(data);
    else if(data<=root->data)
        root->left=insert(root->left,data);
    else
        root->right=insert(root->right,data);
    return root;
}
int search(struct node* root, int data)
{
    if(root==NULL) return 0;
    else if(root->data==data) return 1;
    else if(data<=root->data) return search(root->left,data);
    else return search(root->right,data);
}

这是一个基本的二叉树插入和搜索,有人可以帮助修复此代码以摆脱以下我似乎无法解决的错误。 --->从类型'struct node'分配给类型'struct node *'时不兼容的类型 --->在 '{' 标记

之前应有 '='、','、';'、'asm' 或 'attribute'

【问题讨论】:

  • 为什么你的函数名有括号??

标签: c algorithm binary-tree


【解决方案1】:

struct node(*new_node)(int);

您将 变量 new_node 声明为一个指向函数的指针,该函数接受一个参数并返回 struct node

你想要的是将函数声明(然后定义)为:

struct node*new_node(int);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2014-12-21
    • 2023-03-08
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多