[英]Binary search tree and structure
#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 '
和
struct node(*new_node)(int);
您將變量new_node
聲明為指向 function 的指針,它接受一個參數並返回一個struct node
。
您想要的是將 function 聲明(然后定義)為:
struct node*new_node(int);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.