【问题标题】:Is this C implementation a linked list?这个 C 实现是一个链表吗?
【发布时间】:2021-12-30 14:07:01
【问题描述】:

我是一名 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 是不是一个链表。谢谢!

【问题讨论】:

  • 这是一棵树,因为它正在分叉,在您的代码中有一个提示:(temp->next_dfile) = create_tree
  • 此代码已损坏。您的 create_tnode 函数隐藏了指向 dr->d_name 内容的指针,而不是文件的实际名称。只要您再次调用readdir(dir),该指针就会失效,并且您已经隐藏了一个指向不再存在的东西的指针。一个简单的解决方法是将temp->name = n;更改为temp->name = strdup(n);,但是这段代码还有其他问题(目录在哪里关闭?free在哪里匹配calloc?)。
  • 顺便说一句:你的函数会泄漏内存和文件描述符(你永远不会调用 closedir())另外:递归受限于可用文件描述符的数量;一些风格上的改变可以将行数减少约 50%。
  • @David,Renat 和 wildplasser,谢谢!我将使用 scanddir() 而不是 readdir() 因为我需要按字母顺序。但是放置 closedir() 的最佳位置在哪里?返回之前(ptr_tstart)?

标签: c linked-list tree


【解决方案1】:

这是一棵树。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2014-08-12
    • 2014-09-23
    • 2015-01-04
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多