Итак, я пытаюсь создать программу с несколькими дочерними элементами, в которой есть несколько дочерних элементов, например:
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;
}
}
также изображение в качестве примера:
вот пример того, что я пытаюсь сделать. Как видите, второй «текст» входит в моего первого ребенка.
спасибо за вашу помощь, с трудом изучая связанный список: D!