【发布时间】:2011-09-05 03:40:46
【问题描述】:
我有以下功能(对于我糟糕的 Ansi-C 技能或缺乏技能,我深表歉意):
// Stick a PacketNode into a HashTree
void InsertPacket(IPv4 database, int treeIndex, int hash, Packet packet)
{
// Check to see if the HashTree already has a BST for this hash, and
// create one if not.
if ((*database->hashTrees[treeIndex])->bst == NULL)
{
printf("hashTree[%d]->bst is NULL\n", treeIndex);
Tree newTree;
newTree = InitTree();
newTree->key = hash;
(*database->hashTrees[treeIndex])->bst = newTree; //THIS LINE...
}
if ((*database->hashTrees[treeIndex])->bst != NULL)
{
printf("hashTree[%d]->bst is NOT NULL\n", treeIndex);
}
// Insert the PacketNode into the BST
Tree node;
node = InitNode(hash, packet);
TreeInsert((*database->hashTrees[treeIndex])->bst, node); //OR THIS ONE...
InorderTreeWalk((*database->hashTrees[treeIndex])->bst);
}
问题是我想在函数 3 上执行最后一个 InorderTreeWalk() 函数。 (即我调用Store(database,packet),它调用了InsertData()函数,该函数调用了上面的InsertPacket()函数,我想在Store之后调用tree walk) 在 InsertData 函数中,我初始化并设置了 database->hashTree[treeIndex] = &newHashTree,然后调用 InsertPacket() 创建一个 BST,它是 HashTree 结构的一部分。
我想存储几百个这样的数据包,然后在循环的 Store(database, packet) 调用之后运行 InorderTreeWalk() 函数。
我不确定我是否提供了足够的信息,而且我知道我正在扼杀 C 指针。在过去 3 年多的时间里,我一直主要使用 C# 和 Python 进行编码......“我所有的基础都属于”其他人。
任何建议将不胜感激。
PS:数据库是一个结构体,它有一个指针数组,hashTable[256],用于结构体 HashTrees。其中又包含一个 int 和一个二叉搜索树 bst。 BST 以整数为键,并有一个结构数据包作为数据。数据包只包含几个字符数组。
【问题讨论】:
-
那么您遇到的具体问题是什么?抱歉,我无法解读第一段开头的连续句子。
-
只是为了澄清:第一个 if 语句:如果它是 null 打印并分配一个新的。第二个 if 语句(不是别的????
标签: c pointers struct pass-by-reference parameter-passing