2017-04-19 132 views
-1

我不会错过任何}或;那么为什么我得到这个错误? 实际错误显示“输入结束时的预期声明或声明”。C错误:在输入结束时的预期声明或陈述c

bool search(struct bstnode* root,int data) { 
    if(root == NULL) { 
     return false; 
    } 
    else if(root->data == data) { 
     return true; 
    } 
    else if(data <= root->data) { 
     return Search(root->left,data); 
    } 
    else { 
     return Search(root->right,data); 
    } 
}//getting error at this point 

问题的完整代码...二进制搜索树,在搜索功能(下面)中获取错误。提前致谢。

#include<stdio.h> 
#include<stdlib.h> 
#include<stdbool.h> 
#define BOOL bool 

struct bstnode{ 
int data; 
struct bstnode* left; 
struct bstnode* right; 
}; 
struct bstnode* insert(struct bstnode* root,int data); 
//struct bstnode* root=NULL; 
bool Search(struct bstnode* root,int data); 

int main() 
{ 
struct bstnode* root = NULL; 
root = Insert(root,15); 
root = Insert(root,10); 
root = Insert(root,20); 
root = Insert(root,25); 
root = Insert(root,8); 
root = Insert(root,12); 

int number; 
printf("Enter number be searched\n"); 
scanf("%d",&number); 
//If number is found, print "FOUND" 
search(root,number); 
if(search(root,number) == true) printf("Found\n"); 
else printf("Not Found\n"); 
} 

struct bstnode* getnewnode(int data) 
{ 
struct bstnode* newnode=(struct bstnode*)malloc(sizeof(struct bstnode*)); 
newnode=(struct bstnode*)malloc(sizeof(struct bstnode*)); 
newnode->data=data; 
newnode->left=NULL;//Initially Null, not pointing at anything 
newnode->right=NULL;//Initially NULL, not pointing at anything 
return newnode; //Return address of New Node 
} 

struct bstnode* insert(struct bstnode* root,int data) { 
{ 
if(root==NULL)//condition 1, tree is empty. 
{ 
root=getnewnode(data);//get address of newNode in root 
} 

else if(data<= root->data) 
{ 
root->left=insert(root->left,data); 

} 
else 
{ 
root->right=insert(root->right,data); 
} 
return root; 
} 

bool search(struct bstnode* root,int data) { 

if(root == NULL) { 
return false; 
} 
else if(root->data == data) { 
return true; 
} 
else if(data <= root->data) { 
return Search(root->left,data); 
} 
else 
{ 
return Search(root->right,data); 
} 
}//getting error here. 
+0

将确切的错误复制到问题中。没有解释。 – StoryTeller

+0

这是文件中的最后一个功能吗? – StoryTeller

+0

是的,最后一个功能。错误说:“输入结束时的预期声明或声明”。 – harsher

回答

1

我已经解决了您的代码中的语法错误。您已在insert()函数中添加{两次顶部。另外,search()insert()函数上的错字错误。

#include<stdio.h> 
#include<stdlib.h> 
#include<stdbool.h> 
#define BOOL bool 

struct bstnode 
{ 
    int data; 
    struct bstnode* left; 
    struct bstnode* right; 
}; 
struct bstnode* insert(struct bstnode* root,int data); 
//struct bstnode* root=NULL; 
bool search(struct bstnode* root,int data); 

int main() 
{ 
    struct bstnode* root = NULL; 
    root = insert(root,15); 
    root = insert(root,10); 
    root = insert(root,20); 
    root = insert(root,25); 
    root = insert(root,8); 
    root = insert(root,12); 

    int number; 
    printf("Enter number be searched\n"); 
    scanf("%d",&number); 
    //If number is found, print "FOUND" 
    search(root,number); 
    if(search(root,number) == true) printf("Found\n"); 
    else printf("Not Found\n"); 
} 

struct bstnode* getnewnode(int data) 
{ 
    struct bstnode* newnode=(struct bstnode*)malloc(sizeof(struct bstnode*)); 
    newnode=(struct bstnode*)malloc(sizeof(struct bstnode*)); 
    newnode->data=data; 
    newnode->left=NULL;//Initially Null, not pointing at anything 
    newnode->right=NULL;//Initially NULL, not pointing at anything 
    return newnode; //Return address of New Node 
} 

struct bstnode* insert(struct bstnode* root,int data) { 

    if(root==NULL)//condition 1, tree is empty. 
    { 
     root=getnewnode(data);//get address of newNode in root 
    } 

    else if(data<= root->data) 
    { 
     root->left=insert(root->left,data); 

    } 
    else 
    { 
     root->right=insert(root->right,data); 
    } 
    return root; 
} 

bool search(struct bstnode* root,int data) { 

    if(root == NULL) { 
     return false; 
    } 
    else if(root->data == data) { 
     return true; 
    } 
    else if(data <= root->data) { 
     return search(root->left,data); 
    } 
    else 
    { 
     return search(root->right,data); 
    } 
} 
+0

你应该描述你改变了什么...... – LPs

+0

谢谢你,错误是什么? – harsher