0

我有一个表示图表的数组。我想将它转换为类型的邻接列表(i,j)i<j所以没有双重数据)。到目前为止,我有一个问题:

  1. 我不知道如何访问数组和修改它,我还没有编译下面的代码,但我很确定list[node]->..是不正确的..

到目前为止,这是我的代码,没有数组:

struct node {
    public:
        int number;
        node* next;
};

class AdjList {
    public: 
        void initialize (node* list);
        void convert (bool** array, node* list);
        void make (node*list, int number, int node);
    private:
};

void AdjList::initialize (node* list) {
    for (int i=0; i<10; i++) {
        list[i]->next = NULL;
    }
}

void AdjList::make(node* list, int num, int node) {
    if (list[node]->next == NULL) {
        node* New = new node*;
        list->[node]->next = New;
        New->next = NULL;
        New->number = num;
    }
    else {
        node* New = list[node];
        while (New->next != NULL)
            New = New->next;
        node* New2 = new node*;
        New->next = New2;
        New2->number = num;
    }
}

void AdjList::convert(bool** array, node* list) {
    for (int i=0; i<10; i++) {
        for (int j=i+1; j<10; j++) {
            if (array[i][j] == true)
                make(list, j, i);
        }
    }
}

int main () {
    node* list[10];
}
4

1 回答 1

0

我会把它放在这里,也许它会对某人有所帮助:

for (int i=0;i<bound;i++) {
    for (int j=i+1;j<bound;j++) {
        help = list[i];
        if (Array[i][j] == true) {
            help2 = new node;
            if (help->next == NULL) {
                help->next = help2;
                help2->number = j;
                help2->next = NULL;
            }
            else {
                while (help->next != NULL)
                    help = help->next;
                help->next = help2;
                help2->next = NULL;
                help2->number = j;
            }
        }   
    }
}

我就是这样做的。

于 2013-04-12T14:51:46.753 回答