-4
我基本上构建了一个可以在4个不同方向上传输的链表。重要概念如下:具有4个指针的节点的递归插入函数返回null
Node
在此方案中的一类,有四个Node
指针作为类成员。Data
和Coordinates
也是类。Data
只是节点将包含的数据。raw_coordinates
是新节点应该具有的坐标。Position
是当前节点的坐标。moveNorth
,moveSouth
,moveEast
和moveWest
返回反映位置变化的新坐标。例如,位置节点可能具有坐标(0,0)。moveNorth
取得位置并返回(0,1)。
最后,我想说,我不关心子节点不指向父节点。 (即,如果节点node-> north具有返回到前一个节点的有效节点→南,则不感兴趣)。我的函数返回null
。这里是我的代码:
Node* Map::insertNode(Node *node, Data raw_data, Coordinates raw_coordinates, Coordinates position)
{
if (node == NULL)
{
if (compare_coordinates(raw_coordinates, position))
{
return (newNode(raw_data, raw_coordinates));
}
else
{
return node;
}
}
else
{
if (insertNode(node->north,raw_data, raw_coordinates, moveNorth(position)) != NULL)
{
node->north = insertNode(node->north, raw_data, raw_coordinates, moveNorth(position));
}
else if (insertNode(node->south,raw_data, raw_coordinates, moveSouth(position)) != NULL)
{
node->south = insertNode(node->south, raw_data, raw_coordinates, moveSouth(position));
}
else if (insertNode(node->west, raw_data, raw_coordinates, moveWest(position)) != NULL)
{
node->west = insertNode(node->west, raw_data, raw_coordinates, moveWest(position));
}
else if (insertNode(node->east, raw_data, raw_coordinates, moveEast(position)) != NULL)
{
node->east = insertNode(node->east, raw_data, raw_coordinates, moveEast(position));
}
return node;
}
}
你的问题是什么?你从字面上只发布了你的pgoram的描述。 – Rakete1111
当使用调试器逐步完成代码时,您观察到了什么? – user0042
那么我不断收到一个分段错误,所以我想知道是否有人可以帮我修复它。 –