简体   繁体   English

C-双链表

[英]C - Doubly linked list

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 and co.. Typedef and co ..

    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;

Now, why when i'm doing this: 现在,为什么当我这样做:

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);

Only the first element is displayed. 仅显示第一个元素。
I know this is a very amateur question with what I'm sure is going to be a very simple answer, but I can't resolve the problem. 我知道这是一个非常业余的问题,我确定这将是一个非常简单的答案,但是我无法解决该问题。

I'm not a C expert, but this part doesn't make sense for a linked list: 我不是C专家,但是这部分对于链接列表没有任何意义:

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

It should be 它应该是

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

I counldn't test this, since I'm not sure how to get your code to run. 我不确定该如何测试,因为我不确定如何运行您的代码。

The image below shows the step by step breakdown of whats happening with your link list append operations. 下图显示了链接列表附加操作的详细步骤。

Basically the first time round head and tail point to the new element which has NULL prev and next pointers. 基本上,第一次圆头和尾部指向具有NULL prev和next指针的新元素。

The second time round the head points to the old item and the tail points to the new item. 第二次,头指向旧项目,尾巴指向新项目。 The new item has both prev and next set to the existing item. 新项目将上一个和下一个设置为现有项目。 Only the prev should point to the old item, next should be NULL. 只有上一个应该指向旧项,下一个应该为NULL。 You must also update the old item next pointer to point to the new item which you have not done. 您还必须更新旧项目的下一个指针,以指向尚未完成的新项目。

As it stands the old element is still first in the list and the next pointer is still NULL so you only get one output. 按照原样,旧元素仍然在列表中排在首位,下一个指针仍然为NULL,因此您只会得到一个输出。

逐步添加到链表

Your append code should probably be like this: 您的附加代码可能应该是这样的:

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

(But I have not tested that.) (但是我还没有测试过。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM