2016-07-05 7 views
1

から削除することはできません仕事。私は間違って何をしていますか?は、だから私はこの<code>struct</code>を持って単独リンクリスト

+0

場合の電流>ネクスト>ページ==次に、次>>次=電流 - >ネクスト電流 - X。 –

+0

ヘッドポインタには何も保存しないでください。リストの最初の要素のアドレスを指すポインタ –

+0

@ KishanKumar最後の要素を削除したい場合はどうすればよいですか? – Kidus

答えて

1

まず、リンクされたリストからノードを削除するロジックを理解してください。 x1-> x2-> x3-> x4というリンクリストがあり、そこからx3を削除したいとします。したがって、x3を削除するには、nextのx2のポインタがx4を指すようにするだけです。しかし、これを行う前に、ノードx3に割り当てられたメモリを解放する必要があります。

ここでこれを実現する簡単なコードである:

void delete(int x) { 
struct Book *current; 
for(current = start; current; current = current->next) { 
if(current->next != NULL){ 
if(current->next->pages == x) { 
    Struct Book * temp = current->next; //save the address of the node to be deleted 
    current->next = current->next->next; //remove the node from the linked list 
    free(temp->author); 
    free(...); //whatever memory you want to release 
    free(temp); //free the memory 
    break; 
    } 
} 
} 
} 

削除するノードがリストの先頭またはルートノードである場合をカバーしていない上記の実装。

void delete(int x) { 
if(start == NULL) 
    return; 
struct Book *current; 
if(start->pages == x) 
{ 
    Struct Book * temp = start; //save the address of the node to be deleted 
    start = start->next; //remove the node from the linked list 
    free(temp->author); 
    free(...); //whatever memory you want to release 
    free(temp); 
    return; 
} 
for(current = start; current; current = current->next) 
{ 
if(current->next != NULL){ 
if(current->next->pages == x) { 
Struct Book * temp = current->next; //save the address of the node to be deleted 
current->next = current->next->next; //remove the node from the linked list 
free(temp->author); 
free(...); //whatever memory you want to release 
free(temp); //free the memory 
break; 
} 
} 

}}

+0

リストに要素が1つしかない場合は機能しません。 – Kidus

+0

はいそうです。 LLに削除すべきノードが1つしかない場合。最初のif条件が実行されるため、startはnullを指します。 –

+0

申し訳ありませんが、動作します。 Thanks – Kidus

1

あなたはそれがあるとして、あなたはあなたが実際にリストからノードを削除する必要がfree(current)

void delete(int x) { 
struct Book *current; 
for(current = start; current; current = current->next) { 
    if(current->next != NULL){ 
    if(current->next->pages == x) { 
     current->next = current->next->next; 
     break; 
    } 
    } 
else if(current->pages == x){ 
free(current); 
current = NULL; 
} 

} 

}

+0

私はそれが単独でリンクされたリストだと思う。また、以前の要素が格納されていない –

+0

あなたは正しいです。彼は自分でそれを保存する必要があります – CIsForCookies

+0

yeah @noob。 OK。 –

1

を使用し、その後previous->next = current->next;ともif(current->pages == x)を必要とするリストを残しています。また、リスト構造からリストノードを削除するメソッドと、実際にそれを削除するメソッドにロジックを分割する方がよいでしょう。

struct Book* unlink(int x, struct Book *root) { 
    struct Book *current = root, *prev = root, *next = NULL; 
    while (current) { 
     if(current->pages == x) { 
      next = current->next; 
      if (prev != NULL) { 
       prev->next = next; 
      } 
      return current; 
     } 
     prev = current; 
     current = current->next; 
    } 
    return NULL; 
} 

int delete(int x, struct Book* root) { 
    struct Book *found = unlink(x, root); 
    if (found != NULL) { 
     free(found->title); 
     free(found->actor); 
     free(found); 
     return 0; 
    } 
    return -1; 
} 
関連する問題