typedef struct s_path
{
struct s_path *next;
struct s_path *leaf;
struct s_path *root;
char *path;
t_files_attrib *attrib;
} t_path;
typedef struct s_files_attrib
{
struct s_files_attrib *next;
struct s_files_attrib *previous;
char *filename;
time_t timestamp;
char permissions;
char *owner_name;
char *group_name;
size_t file_size;
size_t link_count;
unsigned int filetype;
t_bool is_soft_link;
char *link_pointer;
} t_files_attrib;
我有2种结构,并且有从一开始就释放t_files_attrib
列表的功能。
第一个结构实现文件系统树。
Example of tree starting from /。
树的叶子是链表,它将所有文件存储在此文件夹中。如果文件不是文件夹或空文件夹,则没有叶。
我如何遍历此结构以释放它?
或者,我该如何重构代码以使树具有大量的叶子?
例如我有这样的文件夹
tests/
├── 123456789111111111111111111111
├── 12345678911111111111111111111111111
├── 12345678911111111111111111111111111111111
├── a
├── ls_out
├── test-fld1
│ ├── a
│ ├── b
│ └── c
└── test-fldr2
void ft_path_append_vertical(t_path *pre, char *name)
{
t_path *path;
path = malloc(sizeof(t_path *));
path->root = pre;
path->next = NULL;
path->path = name;
path->root = path;
if (pre)
pre->leaf = path;
}
t_path *ft_path_append_horizontal(t_path *node, char *dat)
{
t_path *nt;
nt = malloc(sizeof(t_path *));
if (dat)
nt->path = ft_strdup(dat);
nt->next = 0x0;
if (!node)
return (nt);
node->next = nt;
return (nt);
}
所以我的结构将如下所示:
tests->leaf=123456789111111111111111111111;
123456789111111111111111111111->leaf = 12345678911111111111111111111111111;
.
.
.
test-fld1->leaf = a;
a->root = test-fld1;
a->next=b;
b->next=c;
c->next = NULL;
答案 0 :(得分:0)
我认为您具有t_files_attrib的免费功能。假设是 名为 free_files_attrib 。我认为在释放节点之前使用它。 您可以定义一个名为fr_free的函数,以释放所有节点(输入是树的根)。而且我们可以做的自由机制类似于后订单的自由机制。
void ft_free(t_path *root)
{
if (root == NULL) return;
ft_free(root->leaf);
ft_free(root->next);
if (root->leaf == NULL && root->next == NULL)
{
free_files_attrib(root->attrib);
free(root);
root = NULL;
return;
}
}
要使用它,可以在 test 指针上调用ft_free。假设测试是您环境的根源。
ft_free(test);