2016-10-13 5 views
3

私の削除機能を定義している行で、「型が矛盾しています」というエラーが表示されます。 このエラーのほとんどのケースは、宣言の前に関数を呼び出している間に発生します。 しかし、私は、main関数のremove()を事前に定義している間に呼び出しています。 次に、なぜエラー?Error: 'remove'の型が矛盾しています

#include<stdio.h> 
    #include<stdbool.h> 

    struct node 
    { 
     int data; 
     struct node* left; 
     struct node* right; 
    }; 

    struct node* newNode(int x) 
    { 
     struct node* temp=(struct node*)malloc(sizeof(struct node)); 
     temp->data=x; 
     temp->left=NULL; 
     temp->right=NULL; 
     return temp; 
    } 

    struct node* insert(struct node* root,int x) 
    { 
     if (root==NULL) 
      root=newNode(x); 
     else if (x<=root->data) 
      root->left=insert(root->left,x); 
     else 
      root->right=insert(root->right,x); 
     return root; 
    } 

    struct node* remove(struct node* root,int x) 
    { 
     if (root==NULL) 
      printf("Node not found !\n"); 
     else if (x==root->data) 
     { 
      struct node* temp=root; 
      root=NULL; 
      free(temp); 
      printf("Node removed !\n"); 
     } 
     else if (x<=root->data) 
      root->left=remove(root->left,x); 
     else 
      root->right=remove(root->right,x); 
     return root; 
    } 

    bool search(struct node* root,int x) 
    { 
     if (root==NULL) 
      return false; 
     else if (x==root->data) 
      return true; 
     else if (x<=root->data) 
      return search(root->left,x); 
     else 
      return search(root->right,x); 
    } 

    void main() 
    { 
     struct node* root=NULL; 
     root=insert(root,20); 
     root=remove(root,10); 
     root=insert(root,8); 
     root=remove(root,10); 
     root=insert(root,22); 
     root=remove(root,22); 
     root=insert(root,21); 
     root=remove(root,10); 
     root=insert(root,12); 
     root=remove(root,12); 
     root=insert(root,16); 
     root=remove(root,10); 
     root=insert(root,0); 
     root=remove(root,10); 
     root=insert(root,11); 
     root=remove(root,10); 
     root=remove(root,11); 
     printf(search(root,10)?"Found\n":"Not Found\n"); 
     printf(search(root,20)?"Found\n":"Not Found\n"); 
     printf(search(root,11)?"Found\n":"Not Found\n"); 
     printf(search(root,17)?"Found\n":"Not Found\n"); 
    } 

答えて

2

あなたのコードをコンパイルし、私はこれを取得:あなたが見ることができるように

/tmp/x1.c:32: error: conflicting types for ‘remove’ 
/usr/include/stdio.h:154: error: previous declaration of ‘remove’ was here 

を、removeという名前stdio.hに宣言された関数があります。それはあなたが持っている定義と矛盾します。

stdio.hの定義と重複しないように関数の名前を変更する必要があります。

+0

あなたは、天才です! 1トンを調べる。 – iamrkcheers

+0

@iamrkcheers喜んで私は助けることができました。あなたが役に立つと思ったら、[この回答を受け入れる](http://stackoverflow.com/help/accepted-answer)を自由に感じてください。 – dbush

関連する問題