2017-02-15 13 views
0
/* 
Here is the piece of code causing segmentation fault 
*/ 

int search_for_data(T_NODE head, int data){ 
    while(head){ 
     if(head->data > data) 
      head = head->left; 
     if(head->data < data) 
      head = head->right; 
     else 
      return head->key; 
     } 
return -999999;// in case the node is not found 
} 

コードは少数の値に対してセグメンテーションフォルトを投げているようですが、他の人はうまく機能します。私は22の検索を試みましたが、セグメンテーション違反がありました。 headが最初ifNULLになっているかもしれないが、それは、すぐに2番目の最初のifを評価し、元のコードでバイナリ検索ツリーのセグメンテーションフォールト

if(head->data > data) 
     head = head->left; 
    else if(head->data < data) /* this line */ 
     head = head->right; 
    else 
     return head->key; 

答えて

2

そこはifelseが不足しています。

+0

Thanx a lot !! これは本当に役に立ちました! –

関連する問題