我正在尝试编写一个在C ++中执行BFS的函数。我已经为我的队列使用了一个节点类型数组(树的节点)。我收到3个错误,说“无法将节点转换为节点*”,反之亦然。有人可以解释如何纠正这个以及何时使用node *以及何时在使用指针时使用节点。我附加了levelorder和队列实现的代码
//bfs function
void levelorder(node *r)
{
if(r==NULL)
return;
else
{
if(r->lcl!=NULL)
enqueue(r->lcl);
if(r->rcl!=NULL)
enqueue(r->rcl);
cout<<r->n<<" ";
node *k=dequeue();
levelorder(k);
}
}
int h=-1,t=-1; //head and tail for queue
//node structure
struct node{
int n;
node *rcl;
node *lcl;
} b[15]; //b is the queue
void enqueue(node i)
{
if(h==-1)
{
h++;
t++;
b[h]=i;
}
else if(t==49)
{
cout<<"FULL"<<endl;
}
else
{
t++;
b[t]=i;
}
}
node *dequeue()
{
if(h==-1)
{
cout<<"EMPTY ";
return NULL;
}
else
{
return b[h++];
}
}
答案 0 :(得分:0)
“无法将节点转换为节点*”,反之亦然。
从节点转换为节点*
node n; // auto var node
node* nP = &n; // a pointer to that node
从节点*转换为节点
node* nP = new node;
node n2 = *nP; // copy the node into n2
或通过derefencing直接使用nP
foo(node* nP) {
std::cout << nP->right << std::endl;
}
或从非空指针
创建引用node& nR = *np;
何时使用其中一种?
我可以互换地使用它们......但是按大小偏向,指针(节点*)小于(节点)。