2017-12-23 11 views
2

私はリンクされたリスト(ノード)を作成するプログラムを書いています。リンクされたリストには、データと次のアドレスが含まれます。C:リンクされたリストを反転する際の問題

typedef struct node{ 
    int data; 
    struct node *next; 
}node; 

まず、リンクリストを作成します。

struct node *Insert_value(int dataInput,node* head) 
{ 
    node *new_node=NULL; 
    new_node = malloc(sizeof(node)); 
    new_node -> next = head; 
    new_node -> data = dataInput; 
    head = new_node; 
    return head; 
} 

その後、これらのデータを印刷する関数を作成します。 (私はそれをPrintNodeと呼んだ)

while(head!= NULL) 
     { 
      printf("%d\t",head->data); 
      head= head->next; 
     } 
     printf("\n"); 
} 

最後に、リンクリストを逆転させるために作成された関数。

struct node* Reversing(node **head) 
{ 
    node *current, *previous, *first; 
    current = previous = first = *head; 

    first = first->next->next; 
    current = current->next; 
    previous ->next = NULL; 
    current->next = previous; 

    while(first != NULL) 
    { 
     previous = current; 
     current = first; 
     first = first -> next; 
     previous->next = current; 
    } 

    return current; 
} 

私のフルプログラムです。

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

typedef struct node{ 
    int data; 
    struct node *next; 
}node; 

struct node *Insert_value(int dataInput,node* head); 
struct node * Reversing(node **head); 
void PrintNode(node *head); 

main() 
{ 
    node *head = NULL; 
    int i=0,dataInput; 
    while(i!=5) 
    { 
     printf("input your elements: "); 
     scanf("%d",&dataInput); 
     head = Insert_value(dataInput,head); 
     i++; 
    } 
    PrintNode(head); 
    head = Reversing(&head); 
    PrintNode(head); 


}    

struct node *Insert_value(int dataInput,node* head) 
{ 
    node *new_node=NULL; 
    new_node = malloc(sizeof(node)); 
    new_node -> next = head; 
    new_node -> data = dataInput; 
    head = new_node; 
    return head; 
} 

struct node* Reversing(node **head) 
{ 
    node *current, *previous, *first; 
    current = previous = first = *head; 

    first = first->next->next; 
    current = current->next; 
    previous ->next = NULL; 
    current->next = previous; 

    while(first != NULL) 
    { 
     previous = current; 
     current = first; 
     first = first -> next; 
     previous->next = current; 
    } 

    return current; 
} 

void PrintNode(node* head) 
{ 
    while(head!= NULL) 
     { 
      printf("%d\t",head->data); 
      head= head->next; 
     } 
     printf("\n"); 
} 

多くの時間をデバッグした後、私はこれらの機能が正常であることを知っています。ただし、逆関数の後、head変数の次のノードのアドレスはNULLです。あなたは私に説明してアドバイスをくれますか?

+1

node *next = current->next)事前に次のノードを保存する必要があり、例えば、 'あなたのリストをfree'することを忘れないでください。 'while(head!= NULL){node * victim = head; head = head-> next;無料(被害者)。 } ' –

答えて

3

あなたの問題を解決する1つの行の変更は、(あなたは少し間違ってそれを視覚化します)。

current->next =previous; 

previous->next = current; 

の代わりにあなたのコードは、単一の要素にリンクされたリストのために爆発します。関数Reversing()に適切なチェックを追加してください。単一の要素がある場合first->nextNULLになります。しかしがNULLの場合、未定義の動作となるfirst->next->nextを書きました。あなただけの変わらないリンクでReversing()にリンクリストを作成したが、最後のノードにhead指していた以前の場合


。だからnextNULLでした。

+0

私は参照してください。ありがとう:D –

3

Reversingを変更すると、最後に新しいノードが追加されます。リストを通過するとき、あなたは

struct node* Reversing(node **head) 
{ 
    node *current = *head; 
    node *reverse = NULL; 

    while(current) 
    { 
     node *next = current->next; 

     if(!reverse) 
     { 
      reverse = current; 
      reverse->next = NULL; 
     } 
     else 
     { 
      current->next = reverse; 
     } 
     reverse = current; 

     current = next; 
    } 

    return reverse; 
} 
関連する問題