繁体   English   中英

这个 C 实现是一个链表吗?

[英]Is this C implementation a linked list?

我是 Java 开发人员,我正在学习 C。 我正在做一个项目,它是从 C 中的 linux 执行树命令,我有一个简单的问题。 这个 t_node 是一个链表吗? 对我来说,它看起来像一个链接列表。

这是代码的一部分:

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 将是一个文件或目录,如果 ptd 为真则它是一个目录。 next_file 将是同一目录中的下一个文件/目录,而 next_dfile 将是目录中的下一个文件/文件夹。

我们的 music_dir 包含摇滚和迪斯科文件,movie_dir 包含戏剧和惊悚片文件。 mudic_dir 将 movie_dir 作为 next_file。 并且music_dir 将rock 作为next_dfile,rock 将disco 作为next_file,等等。

我只想知道这个 t_node 是否是一个链表。 谢谢!

这是一棵树。

指向兄弟姐妹的指针存储在链表中。 但总的来说,你有一棵树。

                            |
                            v
                          +---+  +---+  +---+
                          |   |->|   |->|   |
                          +---+  +---+  +---+
                            |      |      |
                +-----------+      |      +----------------+
                |                  |                       |
                v                  v                       v
         +---+  +---+  +---+     +---+  +---+  +---+     +---+  +---+  +---+
         |   |->|   |->|   |     |   |->|   |->|   |     |   |->|   |->|   |
         +---+  +---+  +---+     +---+  +---+  +---+     +---+  +---+  +---+
           |      |
  +--------+      +-------+
  |                       |
  v                       v
+---+  +---+  +---+     +---+  +---+  +---+
|   |->|   |->|   |     |   |->|   |->|   |
+---+  +---+  +---+     +---+  +---+  +---+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM