簡體   English   中英

如何遍歷多級鏈表?

[英]How to traverse a multi-level linked list?

我正在編寫一個 C 程序來保存公司經理和員工的數據。

該結構是一個多級鏈表。 每個經理都可以包含內部經理和員工。

這是結構

typedef struct Node {
    char* name;
    NODE* next;
    NODE* down;
        NODE* parent; 
    int is_manager;
} NODE;

在我的程序中,我想遍歷結構,以便我可以找到特定的員工,向他們添加數據,刪除他們等。

添加/刪除功能很簡單 - 但我一直在思考如何在這個結構中遍歷和搜索。

在此先感謝您的任何幫助。

擴展我的例子:

void print_node(NODE *node)
{
    // Tread node as the head of a list, and iterate over that list the "normal" way
    while (node)
    {
        // But also go down the "tree"...
        if (node->down)
        {
            print_node(node->down);
        }

        // Print the name
        printf("%s ", node->name);

        // And go to the next node in the list
        node = node->next;
    }
}

通過遵循jdweng 的建議,您自己解決這個問題應該不難。 用筆和紙把它全部畫出來,然后“手動”打印那棵樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM