C++ 有什么更好的编码方法?
我正在尝试制作一个使用队列实现广度优先搜索的程序C++ 有什么更好的编码方法?,c++,casting,queue,breadth-first-search,C++,Casting,Queue,Breadth First Search,我正在尝试制作一个使用队列实现广度优先搜索的程序 // Queue.h #pragma once #include<iostream> struct node { int b1,b2,b3,b4; node* link; }; struct state { int b1,b2,b3,b4; state* rightChild; state* leftChild; }
// Queue.h
#pragma once
#include<iostream>
struct node
{
int b1,b2,b3,b4;
node* link;
};
struct state
{
int b1,b2,b3,b4;
state* rightChild;
state* leftChild;
};
class Queue
{
private:
node* front, *rear ;
public:
bool enqueue(node n);
//......
};
我创建的队列存储类型为node的节点,而树中的节点类型为State。我不能使用复制构造函数,因为两者都是单独的结构。那么,如何有效地组织代码以使树的节点在队列中被接受呢?使用标准库。这当然是最好的编码方式,因为它既有队列又有查找的可能性。不要在节点中重复状态值,让节点包含一个状态作为成员。
//BFS.cpp
#include <iostream>
#include "Queue.h"
int main()
{
struct state
{
int b1,b2,b3,b4;
state* rightChild;
state* leftChild;
};
// now i define all the nodes of the tree, each node is of type state
Queue Q; // make a queue
Q.enqueue(initialState); //where initialState is the root
//...
}