簡體   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