2017-08-03 8 views
0

私はあまりにも新しいデータ構造で、実際に私は昨日始めました。ここでは、コードは次のようになります。リンクリスト(追加と印刷)に問題がある

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

struct node 
{ 
    int x; 
    node *next; 
}; 

void addToList(node *r, int a); 
void printList(node *r); 
int main() 
{ 
    node *root; 
    root = NULL; 

    for (int i = 0; i < 5; i++) 
    { 
     int a; 
     scanf("%d", &a); 
     addToList(root, a); 
    } 

    printList(root); 

    return 0; 
} 

void addToList(node *r, int a) 
{ 
    while (r != NULL) 
     r = r -> next; 

    r = (node *)malloc(sizeof(node)); 
    r -> x = a; 
    r -> next = NULL; 
} 

void printList(node *r) 
{ 
    while (r != NULL) 
    { 
     printf("%d ", r -> x); 
     r = r -> next; 
    } 

    printf("\n"); 
} 

私はプログラムがリストに新しい5つの要素を取得し、それらを印刷し期待しています。しかし、プログラムの終わりには何も起こっていません。私の欠点は何ですか?

+0

それは 'ボイドaddToList(ノード* R、INT A){ 一方でなければならない(!R->次= NULL) R = R - >次に、 r-> next =(node *)malloc(sizeof(node)); r-> next-> x = a; r-> next-> next = NULL; }「 – roottraveller

+0

申し訳ありませんが動作しませんでした。 – Atreidex

+0

最初の要素を定義した後に機能します。常に最初の要素を定義する必要がありますか?完全に空のリストのための方法はありませんか? – Atreidex

答えて

1

あなたはroot = NULLですが、addtoList機能では、root !=NULLをチェックしています。したがって、テストは失敗し、何も追加されません。

void addToList(node *r, int a) { 
     struct node *temp; 
     temp=(struct node *)malloc(sizeof(struct node)); 
     temp->data = a; 
     if (r== NULL) { 
      r = temp; 
      r->next = NULL; 
     } 
     else { 
      temp->next = r; 
      r = temp; 
     } 
} 
+0

しかし、whileループは、最後の要素にあるかどうかをチェックします。 – Atreidex

+0

最初に実行すると、 'root = null'となり、テストは失敗します。 – Mekicha

+0

@Mekicha 'root'がどこを指しているかは変わりません。 – SHG

1

問題がaddToList()機能である: あなたは、代わりにこのような何かを持っている必要があります。あなたがリストのルート・ノードを更新したい場合は、そのようなあなたの関数を定義する必要があります。

void addToList(node **r, int a) 

をそうでない場合、あなたはrootへのポインタを送信し、あなたが関数の内部でやって何でもやっています。しかし、rootの値はmain()には影響せず、それはNULLのままです。

ポインタの値を変更する場合は、ポインターのアドレスをから==>addToList(&root, a);に送信する必要があります。

ここでrootが指しているところを更新できます。しかしそれだけでは十分ではありません。root常にはリストの先頭を指します==>addToList()の最初の呼び出しでのみ更新したいと考えています。

最後の問題は、新しい作成されたノードをリストの最後のノードとして追加することです。最後のノードへの一時的なポインタを保存することで、これを行うことができます。コード内で私のコメントを見る(<<<と私の変化マーク):ここで

void addToList(node **root, int a)     <<< 
{ 
    node *r = *root;         <<< 
    node *last = NULL;        <<< 

    while (r != NULL) { 
     last = r;         <<< 
     r = r -> next; 
    } 

    r = (node *)malloc(sizeof(node)); 
    r -> x = a; 
    r -> next = NULL; 
    if (last == NULL) {        <<< 
     // this is true only on the first call to 
     // addToList, so we update root only once 
     *root = r; 
    } else { 
     // all other times we add the new node to be the last one 
     last->next = r; 
    } 
} 
1

を、最初の間違いは、それが*rootたびの値は更新されませんので、あなたは、グローバルとして*rootポインタ変数を取っていないということです新しいノードが挿入されます。 *rootの値はNULLになります。

以下のコードには、さまざまな間違いを簡単に説明するコメントがあります。

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

struct node 
{ 
    int x; 
    node *next; 
}; 
node *root;  //Declaring the *root as global 

void addToList(int a); 
void printList(); 
//removing the *root as parameter from both the functions 

int main() 
{ 
    root = NULL; 
    for (int i = 0; i < 5; i++) 
    { 
    int a; 
    scanf("%d", &a); 
    addToList(a); 
    } 
    printList(); 
    return 0; 
} 

void addToList(int a) 
{ 
    //Declaring a temporary pointer(*temp) to avoid the value loss of the *root pointer 
    node *temp=root; 

    //Declaring a new node to save the data taken from the user 
    node *nn = (node *)malloc(sizeof(node)); 

    //Assigning the values to the new node(*nn) 
    nn->x=a; 
    nn->next=NULL; 

    //Checking that the root node is NULL or not 
    //If root is empty, then new node is assigned to *root 
    if(root == NULL) 
    { 
     root=nn; 
    } 
    //Else, we will first find the last node of the linklist using the *temp pointer 
    else 
    { 
     while (temp->next != NULL) 
      temp = temp -> next; 

     //Assigning the new node after the last node of the linklist 
     temp->next=nn; 
    } 
} 

void printList() 
{ 
    node *r=root; 
    while (r != NULL) 
    { 
     printf("%d ", r -> x); 
     r = r -> next; 
    } 
    printf("\n"); 
} 
関連する問題