将信息添加到child-> child链接列表

时间:2019-01-02 15:52:04

标签: c linked-list

因此,我正在尝试编写一个带有多个子项的程序,这些程序中包含多个子项,例如:

root = NULL.

-root->child  (this one contain the first file name)
--root->child->child  (this one contain the information inside the file)
-root->child  (second file name)
--root->child->child  (second file information)

所以我的主要问题是将正确的信息放入孩子体内。

所以我想知道我的操作方式是否符合良好的逻辑,还是应该重新从0开始。

这是我的结构:

typedef struct      s_lst
{
    char            *content; //path name 
    int             data;     //path number
    struct s_lst    *next;
    struct s_lst    *child;
}                   t_lst;

这是我的代码:

int main(int ac, char **av)
{
    t_lst *root;
    root = NULL;
    root = new_node(0,0);
    add_child(root, "first-child", 0);
    for (int i = 0; i < 4; i++)
        lst_add(&(root)->child->child, new_node("inside first child child", i));
    add_child(root, "second", 0);
    for (int i = 0; i < 4; i++)
        lst_add(&(root)->child->child, new_node("inside second child child", i));
    ft_print(root);
}

t_lst   *new_node(char *name, int data)
{
    t_lst *new_node;

    if (!(new_node = malloc(sizeof(t_lst))))
        return (0);
    new_node->content = name;
    new_node->data = data;
    new_node->next = NULL;
    new_node->child = NULL;
    return (new_node);
}

t_lst   *add_sibling(t_lst *n, char *name, int data)
{
    if (n == NULL)
        return (NULL);
    while (n->next)
        n = n->next;
    return (n->next = new_node(name, data));
}

t_lst   *add_child(t_lst *n, char *name, int data)
{
    if (n == NULL)
        return (NULL);

    if (n->child)
        return (add_sibling(n->child, name, data));
    else
        return (n->child = new_node(name, data));
}

void    lst_add(t_lst **head, t_lst *new)
{
    t_lst   *tmp;

    if (*head)
    {
        tmp = *head;
        while (tmp->next)
            tmp = tmp->next;
        tmp->next = new;
    }
    else
        *head = new;
}

ft_print:

void    ft_print(t_lst *root)
{
    while (root)
    {
        while (root->child)
        {
            printf("%s\n", root->child->content);
            printf("-----------------------------\n");
            while (root->child->child)
            {
                printf("node->child->child %s\n", root->child->child->content);
                root->child->child = root->child->child->next;
            }
            printf("\n");
            root->child = root->child->next;
        }
        root = root->next;
    }
}

也以图片为例: here is an example what i am trying to do. As you can see the second "text" goes into my first child. 感谢您的帮助,我们很难学习链接列表:D!

1 个答案:

答案 0 :(得分:0)

您的主要问题是,在您的第二个循环中,您要添加到同一个(第一个)孩子中:

for (int i = 0; i < 4; i++)
    lst_add(&(root)->child->child, new_node("inside second child child", i));
//                        ^~~~~~~
//              should be ->next->child here

更好的办法是利用add_child的返回值,这将使逻辑更加清楚:

t_lst *first_child = add_child(root, "first-child", 0);
for (int i = 0; i < 4; i++)
    lst_add(&first_child->child, new_node("inside first child child", i));

t_lst *second_child = add_child(root, "second-child", 0);
for (int i = 0; i < 4; i++)
    lst_add(&second_child->child, new_node("inside second child child", i));

还没有发现的另一个问题是您正在修改ft_print函数中的列表:

void ft_print(t_lst *root)
{
    while (root)
    {
        while (root->child)
        {
            ...
            while (root->child->child)
            {
                ...
                root->child->child = root->child->child->next;
                // reassigning and effectively "loosing" child nodes here
            }
            ...
            root->child = root->child->next;
            // and here
        }
        root = root->next;
    }
}

您应该使用局部变量遍历子节点:

t_lst *current = root->child;
while (current)
{
    ...
    current = current->next;
}