[英]Is this C implementation a linked list?
I'm a Java dev and I'm learning C.我是 Java 开发人员,我正在学习 C。 I'm working on a project, it's the implementation of tree command from linux in C and I have a simple question.
我正在做一个项目,它是从 C 中的 linux 执行树命令,我有一个简单的问题。 Is this t_node a linked list?
这个 t_node 是一个链表吗? It looks like a linked list for me.
对我来说,它看起来像一个链接列表。
This is a part of the code:这是代码的一部分:
struct t_node {
char* name;
int ptd;
struct t_node *next_dfile;
struct t_node *next_file;
};
static struct t_node* create_tree(char *root_name) {
DIR *dir = opendir(root_name);
struct dirent *dr = {NULL};
struct t_node *ptr_tstart = NULL,*temp = NULL,*temp1 = NULL;
char *name = (char *)calloc(2000, sizeof(char));
if (dir == NULL) {
printf("\nFailed to open ..!!");
printf(" : %s", root_name);
return NULL;
}
while ((dr = readdir(dir)) != NULL) {
if (strcmp((dr->d_name), ".") != 0 && strcmp((dr->d_name), "..") != 0) {
temp = create_tnode(dr->d_name);
} else {
temp = NULL;
continue;
}
if (temp1 != NULL) {
temp1->next_file = temp;
} else {
(ptr_tstart) = temp;
}
if ((dr->d_type) == DT_DIR) {
temp->ptd = TRUE;
strcpy(name, root_name);
(temp->next_dfile) = create_tree((strcat((strcat(name, "/")), dr->d_name)));
strcpy(name, root_name);
} else {
(temp)->ptd = FALSE;
(temp)->next_dfile = NULL;
}
temp1 = temp;
}
return (ptr_tstart);
}
static struct t_node* create_tnode(char* n) {
struct t_node *temp = (struct t_node *)malloc(sizeof(struct t_node));
temp->name = n;
temp->next_dfile = NULL;
temp->next_file = NULL;
return temp;
}
t_node will be a file or directory, if ptd is true then it is a directory. t_node 将是一个文件或目录,如果 ptd 为真则它是一个目录。 next_file will be the next file/directory in the same directory and next_dfile will be the next file/folder inside a directory.
next_file 将是同一目录中的下一个文件/目录,而 next_dfile 将是目录中的下一个文件/文件夹。
We have music_dir with rock and disco files and movie_dir with drama and thriller files.我们的 music_dir 包含摇滚和迪斯科文件,movie_dir 包含戏剧和惊悚片文件。 mudic_dir will have movie_dir as next_file.
mudic_dir 将 movie_dir 作为 next_file。 And music_dir will have rock as next_dfile, and rock will have disco as next_file, etc.
并且music_dir 将rock 作为next_dfile,rock 将disco 作为next_file,等等。
I just want to know if this t_node is a linked list.我只想知道这个 t_node 是否是一个链表。 Thank you!
谢谢!
It's a tree.这是一棵树。
The pointers to the siblings are stored in a linked-list.指向兄弟姐妹的指针存储在链表中。 But overall, you have a tree.
但总的来说,你有一棵树。
|
v
+---+ +---+ +---+
| |->| |->| |
+---+ +---+ +---+
| | |
+-----------+ | +----------------+
| | |
v v v
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| |->| |->| | | |->| |->| | | |->| |->| |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| |
+--------+ +-------+
| |
v v
+---+ +---+ +---+ +---+ +---+ +---+
| |->| |->| | | |->| |->| |
+---+ +---+ +---+ +---+ +---+ +---+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.