2017-04-19 150 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