2016-10-24 7 views
0

リンクされたリストの先頭から削除するときにsegfaultが表示されるようです。返される頭部はNULLになります。しかし、何か他の場所を削除することもできますが、printf文が印刷されるので、頭が正しくリセットされていないことが考えられます。それは1回のパスを行い、頭と作品から削除しますが、2回目は削除しません。ヘッドノードを削除するときにリンクリストコードがセグメンテーションフォルトを生成するのはなぜですか?

node_t* delete_it(node_t** head, int id){ 

    node_t* temp; 
    node_t* prev = (*head); 
    node_t* current = (*head); 
    int i = 0; 

    //checking to see if the head has the id 
    if((*head)->player.player_ID == id){ 

     printf("removing from the head\n"); 
     temp = (*head); 
     (*head) = (*head)->next; 
     free(temp); 
     return (*head); 

    } 

    //moving through finding the id 
    while(current->player.player_ID != id){ 

     if(i > 0){ 

      prev = prev->next; 

     } 
     //keeps prev pointer the one before current 
     i++; 
     current = current->next; 

     //checking for tail 
     if(current->next == NULL && current->player.player_ID == id){ 
      temp = current; 
      free(temp); 
      return (*head); 

     } 

     //removing the node form somewhere inbetween head and tail 
     if(current->player.player_ID == id){ 

      temp = current; 
      prev->next = current->next; 
      free(temp); 
      return (*head); 

     } 

     return(*head) 
    } 
} 
+0

機能が終了した場合、私は表示されません。また、代わりにheadへのNULLからprevを初期化し、ロジックを合理化することで、あなたは、実質的にコードを単純化することができます。 1つの閉じる "}"がありません。それを追加します。 – MayurK

+0

あなたはすでにポインタを頭に詰めているので、それを返す必要はありません。 – 2501

+0

@ MichaelEhnes--私の最初の答えは、あなたのコードとは少し異なる構造体のデザインを使用していることに気付きました。私はこれを修正し、動作するコードの修正版を追加しました。 –

答えて

0

delete_it()には多くの問題があります。それはNULLポインタの入力を処理せず、一貫性のある方法でリストをステップ実行せず、リストを横断する際に発生する可能性のあるNULLポインタのすべてのケースを処理せず、目的を果たさない変数iを含み、論理は混乱です。あなたがそれを救うべきではない、

node_t* delete_it_2(node_t** head, int id){ 

    node_t* temp; 
    node_t* prev = (*head); 
    node_t* current = (*head); 
    int i = 0; 

    //checking to see if the head has the id 
    if(*head != NULL && (*head)->player.player_ID == id){ 

     printf("removing from the head\n"); 
     temp = (*head); 
     (*head) = (*head)->next; 
     free(temp); 
     return (*head); 

    } 

    //moving through finding the id 
    while(current != NULL && current->player.player_ID != id){ 
     prev = current; 
     current = current->next; 
    } 

    if (current == NULL) 
     return *head; 

    //checking for tail 
    if(current->next == NULL && current->player.player_ID == id){ 
     free(current); 
     prev->next = NULL; 
     return (*head); 
     } 

    //removing the node form somewhere inbetween head and tail 
    if(current->player.player_ID == id){ 

     temp = current; 
     prev->next = current->next; 
     free(temp); 
     return (*head); 
    } 

    return *head; 
} 

しかし:仕事に救済することができます。あなたのdelete_it()機能は、必要以上に複雑です。私はあなたのように、リストの先頭へのポインタを返すだろうが、その後、頭にダブルポインタを渡す必要はありません。

node_t * delete_it(node_t *head, int id){ 

    node_t *temp; 
    node_t *prev = NULL; 
    node_t *current = head; 

    while (current != NULL && current->player.player_ID != id) { 
     prev = current; 
     current = current->next; 
    } 

    if (current != NULL) { 
     if (prev != NULL) { 
      temp = current; 
      prev->next = current->next; 
     } else { 
      temp = head; 
      head = head->next; 
     } 
     free(temp); 
    } 

    return head; 
} 
関連する問題