c++ - 创建一组随机连接的节点?

标签 c++ data-structures

我正在尝试编写一个函数 generate_labyrinth(),它创建固定数量的随机连接节点。每个节点都与其他三个节点相连。

这是我现在拥有的:

迷宫.h:

#ifndef LABYRINTH_H
#define LABYRINTH_H     
// node
class Room {
public:
    Room () 
        : room_number(0), left_door(nullptr),
        center_door(nullptr), right_door(nullptr) { }

    // data member
    int room_number;
    // pointers to other nodes
    Room* left_door;
    Room* center_door;
    Room* right_door;
};
//================================================= 

class Labyrinth {
public:
    Labyrinth (int s) : size(s) { generate_labyrinth (); }
private:
    // number of nodes
    int size;

    // root node
    Room* entrance;

    // Helper functions
    int random_number (int from, int to);
    int random_number_without_i (int min, int max, int i);

    // Initialiazation function
    void generate_labyrinth ();
};
#include "labyrinth.cpp"
#endif

迷宫.cpp:

// Class Labyrinth member implementations
int Labyrinth::random_number (int min, int max) {
    static bool seed_initialized = false;
    if (!seed_initialized) {
        seed_initialized = true;
        srand((unsigned int) time(NULL));
    }
    return rand() % (max - min + 1) + min; 
}

int Labyrinth::random_number_without_i (int min, int max, int i) {
    int res = random_number(min, max);
    while (res == i){
        res = random_number(min, max);
    }
    return res;
}

void Labyrinth::generate_labyrinth () {
    // create "size" number of nodes
    entrance = new Room[size];    

    // initialize Room (node) data members
    for (auto i = 0; i < size; ++i) {
        entrance[i].room_number = i;
    }
    // connect each room with three others
    int first_room = 1;  
    int last_room = size - 1;
    for (int i = 1; i < size; ++i) {
        // avoid connecting a room with itself            
        int left_goes_to = random_number_without_i (first_room, last_room, i);
        int center_goes_to = random_number_without_i (first_room, last_room, i);
        int right_goes_to = random_number_without_i (first_room, last_room, i);

        entrance[i].left_door = (&entrance[left_goes_to]);
        entrance[i].center_door = (&entrance [center_goes_to]);
        entrance[i].right_door = (&entrance [right_goes_to]);;
    }
    // TEST IF NODES POINT TO EACH OTHER
    for (auto i = 0; i < size; ++i) {
        if (entrance[i].left_door == nullptr || entrance[i].center_door == nullptr ||
            entrance[i].right_door == nullptr) {
            std::cout <<"Uninitialized pointer value\n";
        }
    }
    getchar();
}

主要:

#include <iostream>
#include <time.h>
#include "labyrinth.h"
//=================================================  

int main()
{
    int cave_size = 20;
    Labyrinth cave(cave_size);
}

generate_labyrinth()中节点初始化后出现了三个指针,left_room, center_room, right_room 保持未初始化状态,即我得到的输出是:

Uninitialized pointer value

问题:

为什么节点中的指针没有初始化?

是否有另一种方法来生成一组随机连接的节点?


注意:我没有使用 insert() 函数,因为节点的数量是固定的,并且在数据结构的构建过程中确定。

最佳答案

您为 first_room 生成连接至 last_room , 这是房间 1size-1 .

// connect each room with three others
int first_room = 1;  
int last_room = size - 1;
for (int i = 1; i < size; ++i) {

但是当您检查连接时,您会从房间 0 开始(可能应该是入口本身)。

// TEST IF NODES POINT TO EACH OTHER
for (auto i = 0; i < size; ++i) {

关于c++ - 创建一组随机连接的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838326/

相关文章:

c++ - 阻塞和非阻塞队列

c++ - Boost 线程串行运行,而不是并行运行

algorithm - 非递归实现树高和isBST的伪代码

php - PHP 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义?

data-structures - 哈希表 - 使用二叉搜索树实现

java - 1 位和 2 位字符

c++ - 临时对象不表现为 const

c++ - 析构函数调用中出现信号 11 的可能原因是什么?

c++ - 谁能举例说明为什么使用 size_t 类型?

algorithm - 多维数组之和