I have a defined node type for graph node:
我为图节点定义了一个节点类型:
struct node{
int data;
int neighboursNumber;
node * neighbours;
};
And I am trying to add for each node some neighbours like this:
我试图为每个节点添加一些像这样的邻居:
node * n1 = (node *)malloc(sizeof(node));
n1->data = 1;
n1->neighboursNumber = 2;
n1->neighbours = (node *)malloc(sizeof(node) * n1->neighboursNumber);
node * n2 = (node *)malloc(sizeof(node));
n2->data = 2;
n2->neighboursNumber = 1;
n2->neighbours = (node *)malloc(sizeof(node) * n2->neighboursNumber);
node * n3 = (node *)malloc(sizeof(node));
n3->data = 3;
n3->neighboursNumber = 1;
n3->neighbours = (node *)malloc(sizeof(node) * n3->neighboursNumber);
n1->neighbours[0] = n2;
n1->neighbours[1] = n3;
n2->neighbours[0] = n1;
n3->neighbours[0] = n1;
But I get an error at building
但是我在建筑时遇到了错误
error C2679: binary '=' : no operator found which takes a right-hand operand
of type 'node *' (or there is no acceptable conversion)
Isn't that the correct way to do it?
这不是正确的方法吗?
2 个解决方案
#1
(borrowing) I think this will work for you...
(借用)我认为这对你有用......
typedef struct _node {
int data;
int neighboursNumber;
struct _node **neighbours;
} node;
int main( void ) {
node *n1 = (node *)malloc(sizeof(node));
n1->data = 1;
n1->neighboursNumber = 2;
n1->neighbours = (node **)malloc(sizeof(node *) * n1->neighboursNumber);
node * n2 = (node *)malloc(sizeof(node));
n2->data = 2;
n2->neighboursNumber = 1;
n2->neighbours = (node **)malloc(sizeof(node *) * n2->neighboursNumber);
node * n3 = (node *)malloc(sizeof(node));
n3->data = 3;
n3->neighboursNumber = 1;
n3->neighbours = (node **)malloc(sizeof(node *) * n3->neighboursNumber);
Up to here that is... I am not sure how you are using it but, I don't think your neighbours are aligned in a logical sense... ??
到目前为止......我不确定你是如何使用它的,但我不认为你的邻居在逻辑意义上是对齐的???
n1->neighbours[0] = n2;
n1->neighbours[1] = n3;
n2->neighbours[0] = n1;
n3->neighbours[0] = n1;
#2
Instead of
struct node{
int data;
int neighboursNumber;
node * neighbours;
};
write this:
typedef struct _node{
int data;
int neighboursNumber;
struct _node * neighbours;
} node;
but as others have pointed out in the comments, neighbours should rather be
但正如其他人在评论中指出的那样,邻居应该是
struct _node ** neighbors;
#1
(borrowing) I think this will work for you...
(借用)我认为这对你有用......
typedef struct _node {
int data;
int neighboursNumber;
struct _node **neighbours;
} node;
int main( void ) {
node *n1 = (node *)malloc(sizeof(node));
n1->data = 1;
n1->neighboursNumber = 2;
n1->neighbours = (node **)malloc(sizeof(node *) * n1->neighboursNumber);
node * n2 = (node *)malloc(sizeof(node));
n2->data = 2;
n2->neighboursNumber = 1;
n2->neighbours = (node **)malloc(sizeof(node *) * n2->neighboursNumber);
node * n3 = (node *)malloc(sizeof(node));
n3->data = 3;
n3->neighboursNumber = 1;
n3->neighbours = (node **)malloc(sizeof(node *) * n3->neighboursNumber);
Up to here that is... I am not sure how you are using it but, I don't think your neighbours are aligned in a logical sense... ??
到目前为止......我不确定你是如何使用它的,但我不认为你的邻居在逻辑意义上是对齐的???
n1->neighbours[0] = n2;
n1->neighbours[1] = n3;
n2->neighbours[0] = n1;
n3->neighbours[0] = n1;
#2
Instead of
struct node{
int data;
int neighboursNumber;
node * neighbours;
};
write this:
typedef struct _node{
int data;
int neighboursNumber;
struct _node * neighbours;
} node;
but as others have pointed out in the comments, neighbours should rather be
但正如其他人在评论中指出的那样,邻居应该是
struct _node ** neighbors;