我正在写一个C程序来保存公司经理和雇员的数据。
该结构是一个多级链表。每个经理可以包含内部经理和员工。
这是一个结构
typedef struct Node {
char* name;
NODE* next;
NODE* down;
NODE* parent;
int is_manager;
} NODE;在我的程序中,我想要遍历结构,这样我就可以找到特定的员工,向他们添加数据,删除他们等等。
添加/删除函数很简单--但是我被困在如何在这个结构中遍历和搜索。
提前感谢您的任何帮助。
发布于 2019-06-09 18:57:13
在我的例子中展开:
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;
}
}通过遵循the advice by jdweng,您自己应该不难弄清楚这一点。用纸和笔把它画出来,然后“手动”打印这棵树。
https://stackoverflow.com/questions/56513880
复制相似问题