繁体   English   中英

C 中的结构体、指针和树

[英]Structs, pointers, and trees in C

对于我们最后一个学期的项目,我的操作系统 class 中的每个人都负责实现一个伪“linux 文件系统”。 这个想法是模拟处理文件、文件夹、更改目录等。

当我在 C 中编程时,我不喜欢使用字符串和指针,不幸的是,为了我的安心,这个项目看起来涉及两者。 因为我对指针比较不适应,所以我希望我能对我的底层树结构的后端实现进行健全的检查。

typedef struct floorNode
{
    char floorName[30]; //the name of the tree node
    struct floorNode *parentPointer; //this is a pointer to the parent node. Null for the root node.
    struct floorNode *childPointers[10]; //this is an array holding pointers to up to 10 child nodes. 
    char fileArray[10][30]; //this is an array of 10 'files', each of up to length 30.
                            //for this assignment, strings are the only type of "file"

} floorNode;

这是在 C 中实现树的正确方法吗?

这或多或少是正确的数据类型。

我担心fileArray[][] 我认为不需要它,除非我误解了它的目的。 要获取孩子的floorName ,请改为遍历childPointers[]以获取孩子中的名称。

如果节点有 30 个字符串,需要考虑的是让所有节点的存储空间更大一点,在这种情况下为 31,以便始终存在尾随 NUL,并且不需要特殊的令人讨厌的处理来区分 30 个字符没有 NUL 的字符串和所有具有 NUL 的较短字符串。

你可能想要一个孩子的链接列表。 您绝对不希望为此使用 arrays 指针。 您还应该考虑如何知道文件是否实际上是目录。

暂无
暂无

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

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