2016-04-07 22 views
-1
ConsoleInfo   *CI_new(const char *name, const char *ip, ConsoleInfo *prev, ConsoleInfo *next) 
{ 
    ConsoleInfo *list; 
    if ((list = malloc(sizeof(ConsoleInfo)))) 
    { 
     list->name = strdup(name); 
     list->ip = strdup(ip); 
     list->prev = !prev ? NULL : prev; 
     list->next = !next ? NULL : next; 
    } 
    return (list); 
} 

bool    CIL_is_empty(ConsoleInfoList *list) 
{ 
    return (list->count == 0); 
} 

ConsoleInfoList  *CIL_append(ConsoleInfoList *list, const char *name, const char *ip) 
{ 
    if (CIL_is_empty(list)) 
    { 
     list->head = list->tail = CI_new(name, ip, NULL, NULL); 
    } 
    else 
    { 
     ConsoleInfo *new = CI_new(name, ip, list->tail, list->head); 
     list->tail = new; 
     list->head = new->next; 
    } 
    list->count++; 
    return (list); 
} 

のtypedefとコ...今 C - 双方向リンクリスト

typedef struct ConsoleInfo ConsoleInfo; 
    typedef struct ConsoleInfoList ConsoleInfoList; 

    struct ConsoleInfoList { 
     size_t count; 
     ConsoleInfo *head; 
     ConsoleInfo *tail; 
    }; 

    struct ConsoleInfo { 
     char *name; 
     char *ip; 
     ConsoleInfo *next; 
     ConsoleInfo *prev; 
    }; 

typedef struct ConsoleName 
{ 
    char value[256]; 
} ConsoleName; 

typedef struct ConsoleIp 
{ 
    char value[256]; 
} ConsoleIp; 

が、私はこれを、なぜやっている:

ConsoleInfoList  *CIL_new(void) 
{ 
    ConsoleInfoList *list; 

    if ((list = malloc(sizeof(ConsoleInfoList)))) 
    { 
     list->count = 0; 
     list->head = NULL; 
     list->tail = NULL; 
    } 
    return (list); 
} 


ConsoleInfoList *cil = CIL_new(); 
    if (!cil) 
     return (NULL); 
    ConsoleName name; 
    ConsoleIp ip; 
    for (int i = 0; i < GetNumberOfConsoles(); ++i) 
    { 
     GetConsoleInfo(i, &name, &ip); 
     cil = CIL_append(cil, name.value, ip.value); 
    } 
    for (ConsoleInfo *ci = cil->head; ci; ci = ci->next) 
     CI_print(ci); 

最初の要素だけが表示されます。
これは非常に簡単な答えになると確信している非常に素人の質問ですが、私は問題を解決することはできません。

+0

あなたは 'CIL_new(の定義を示すべきです)'、 ' ConsoleName'と 'ConsoleIp'です。理想的には[sscce](http://sscce.org/)。 – tinman

+0

右:編集しました:) –

+0

定義を追加していただきありがとうございますが、問題の小さなコンパイル可能な例を貼り付けると理想的です。 – tinman

答えて

1

下の画像は、リンクリストの追加操作で起こっていることをステップバイステップで示しています。

基本的には、headとtailの最初の丸めはNULLのprevとnextポインタを持つ新しい要素を指します。

2回目は、元のアイテムを指し、後ろのアイテムは新しいアイテムを指します。新しいアイテムは、既存アイテムと前のセットの両方を持っています。 prevだけが古い項目を指す必要があり、nextはNULLにする必要があります。古い項目の次のポインタを更新して、まだ行っていない新しい項目を指すようにする必要があります。

古い要素はまだリストの先頭にあり、次のポインタはまだNULLなので、1つの出力しか得られません。

Step by step addition to linked list

あなたのアペンドコードは、おそらく次のようにする必要があります:

ConsoleInfo *new = CI_new(name, ip, list->tail, NULL); 
list->tail->next = new; 
list->tail = new; 

(。しかし、私はそれをテストしていない)

+0

本当にありがとう! –

1

私はCの専門家ではないんだけど、この部分は、リンクリストのために意味をなさない:以来、私はこれをテストcounldn't

ConsoleInfo *new = CI_new(name, ip, list->tail, list->head); 
list->tail = new; 
list->head = new->next; 

それは

ConsoleInfo *new = CI_new(name, ip, list->tail, NULL); 
list->tail->next = new 
list->tail = new; 

する必要があります私はあなたのコードをどのように動かすかわからない。

+0

ありがとうございます! –