2016-12-25 15 views
2

私は、以下のリンクリストの実装があります。私は、このリンクリストにこの機能を実行した2つのインデックス間のリンクリストのノードを削除するにはどうすればよいですか?

void deleteList(struct _list *list, int from, int to) { 
    int i; 

    assert(list != NULL); 

    // I skipped error checking for out of range parameters for brevity of code 

    for (i = from; i <= to; i++) { 
     deleteNode(list->head, i); 
    } 
} 

//::このような[First]->[Second]->NULL

struct _node { 
    char *string; 
    struct _node *next; 
} 

struct _list { 
    struct _node *head; 
    struct _node *tail; 
} 

私は次の関数を作りたいのdeleteNodes(list, 1, 1) 2行目を削除して [First]->[Second]->NULLを取得しましたが、このように実行するとdeleteList(list, 0, 1)と入力します[First]->[Second]->[Third]->NULL私はsegの欠陥を得る。

void pop(struct _node *head) { 
    if (head == NULL) { 
     return; 
    } 

    struct _node *temp = head; 
    head = head->next; 
    free(temp); 
} 

が、それは私がワンセグ障害与えますか:ここで

は私のdeleteNode機能は、私がもしまたは0 =へのリンクリストの先頭を削除するには、別の関数を書いた

void deleteNode(struct _node *head, int index) { 
    if (head == NULL) { 
     return; 
    } 

    int i; 
    struct _node *temp = head; 

    if (index == 0) { 
     if (head->next == NULL) { 
      return; 
     } 
     else { 
      head = head->next; 
      free(head); 
      return; 
     } 
    } 

    for (i = 0; temp!=NULL && i<index-1; i++) { 
     temp = temp->next; 
    } 

    if (temp == NULL || temp->next == NULL) { 
     return; 
    } 

    Link next = temp->next->next; 

    free(temp->next); 

    temp->next = next; 
} 

ですメモリエラートラップを中止します。6.

+0

あなたあなたのループ'deleteNode'の呼び出しには欠陥があります:範囲内の最初のノードを削除すると、削除する次のノードは以前と同じインデックスを持ちません。 –

+0

もちろん!だから私は新しい頭部へのポインタを保持する必要がありますか?それとも全く別のアプローチをとるべきですか? – user6005857

+0

単純な解決策は、ループを逆にして、最初の範囲の最後のノードを削除し、最後に最後のノードを削除するなどです。 –

答えて

1

目的のために1つのノードstructを1つだけ使用するとよいです。

struct node { 
    char *string; 
    struct node *next; 
}; 

リストの変更の長さに応じてインデックスを調整しない場合、2つのインデックス間の要素を除去するために、あなたのループは、右の要素を削除しません。そしてあなたはまた、リストの新しい頭を返さなければなりません。

struct node *deleteList(struct node *head, unsigned from, unsigned to) { 
    unsigned i; 
    unsigned count = 0; 
    for (i = from; i <= to; i++) { 
     head = delete_at_index(head, i - count); 
     count++; 
    } 
    return head; 
} 

ヘルプ機能delete_at_indexは以下のようになります。

struct node *delete_at_index(struct node *head, unsigned i) { 
    struct node *next; 

    if (head == NULL) 
     return head; 

    next = head->next; 

    return i == 0 
      ? (free(head), next)         /* If i == 0, the first element needs to die. Do it. */ 
      : (head->next = delete_at_index(next, i - 
               1), head); /* If it isn't the first element, we recursively check the rest. */ 
} 

以下のプログラムを完了してください。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct node { 
    char *string; 
    struct node *next; 
}; 

void freeList(struct node *head) { 
    struct node *tmp; 

    while (head != NULL) { 
     tmp = head; 
     head = head->next; 
     free(tmp->string); 
     free(tmp); 
    } 

} 

struct node *delete_at_index(struct node *head, unsigned i) { 
    struct node *next; 

    if (head == NULL) 
     return head; 

    next = head->next; 

    return i == 0 
      ? (free(head), next)         /* If i == 0, the first element needs to die. Do it. */ 
      : (head->next = delete_at_index(next, i - 
               1), head); /* If it isn't the first element, we recursively check the rest. */ 
} 

struct node *deleteList(struct node *head, unsigned from, unsigned to) { 
    unsigned i; 
    unsigned count = 0; 
    for (i = from; i <= to; i++) { 
     head = delete_at_index(head, i - count); 
     count++; 
    } 
    return head; 
} 

void pushvar1(struct node **head_ref, char *new_data) { 
    struct node *new_node = malloc(sizeof(struct node)); 
    new_node->string = strdup(new_data); 
    new_node->next = (*head_ref); 
    (*head_ref) = new_node; 
} 

void printListvar1(struct node *node) { 
    while (node != NULL) { 
     printf(" %s ", node->string); 
     node = node->next; 
    } 
    printf("\n"); 
} 

int main(int argc, char **argv) { 
    struct node *head = NULL; 
    for (int i = 0; i < 5; i++) { 
     char str[2]; 
     sprintf(str, "node%d", i); 
     pushvar1(&head, str); 
    } 

    puts("Created Linked List: "); 
    printListvar1(head); 
    head = deleteList(head, 0, 2); 
    puts("Linked list after deleted nodes from index 0 to index 2: "); 
    printListvar1(head); 
    freeList(head); 
    return 0; 
} 

すべてのプログラミングの問題は、間接の余分なレベルを追加することによって解決することができるテスト

Created Linked List: 
node4 node3 node2 node1 node0 
Linked list after deleted nodes from index 0 to index 2: 
node1 node0 
+0

? :delete_at_index関数の意味ですか? – user6005857

+0

@ user6005857 'delete_at_index function'は、1つのノーを削除するヘルパー関数です –

+0

私はそれを理解しています。私はちょうど疑問符コロンの構文と混乱していたが、私はそれを理解した。 – user6005857

1

:ポインタへのポインタを使用して...


unsigned deletefromto(struct node **head, unsigned from, unsigned to) 
{ 
unsigned pos,ret; 
struct node *this; 

for (pos=ret=0; this = *head;pos++) { 
     if (pos < from) { head = &(*head)->next; continue; } 
     if (pos > to) break; 
     *head = this->next; 
     free(this); 
     ret++; 
     } 
return ret; /* nuber of deleted nodes */ 
} 
関連する問題