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 /。 Tree的叶子是链式列表,它存储该文件夹中的所有文件。如果file不是文件夹或空文件夹,则它没有叶子。我该如何遍历这个结构来释放它?或者我如何重构代码以使树具有不同数量的叶子?例如,我有这样的文件夹
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;
我认为你有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;
}
}
要使用它,可以在测试指针上调用ft_free。假设测试是您环境的根源。
ft_free(test);