我正在尝试在C中实现广度优先搜索,但在横切我遇到问题时,节点无法保留其访问信息,
这是我的bfs功能的代码。
这是邻接列表。
0->1->2->3->
|
V
1->0->4->5->
|
V
2->0->6->7->
|
V
3->0->7->
|
V
4->1->
|
V
5->1->
|
V
6->2->
|
V
7->2->3->
here rollsTemp, rollsTemp1 are pointing towards first element of the adj list
void bfs(struct hack *rollsTemp, struct hack *rollsTemp1){
struct hack *head = rollsTemp; // we take a temp head
enqueue(rollsTemp1->node); //we enqueue 0 and mark it as visited
rollsTemp1->V = 1;
char Vertex;
while (g_front <= g_rear){ // until the queue is empty
while (rollsTemp != 0){ // rollsTemp is an iterator travells vertically untill the vertex is found
if (rollsTemp == head) { // only dequeue when the iterator is at the top, it prevents dequeueing on very iteration for searching
Vertex = dequeue();
p("%c ", Vertex); // prints 0
}
if (rollsTemp->node == Vertex){ // here we find that 0 is on the first row of the adj list
rollsTemp1 = rollsTemp; // we set another pointer to transverse horizontally
while (rollsTemp1 != 0){
if (rollsTemp1->V == 0){
rollsTemp1->V = 1;
enqueue(rollsTemp1->node);
rollsTemp1 = rollsTemp1->next;
} else {
rollsTemp1 = rollsTemp1->next;
}
}
rollsTemp = head;
} else {
rollsTemp = rollsTemp->down;
}
}
}
}
我进一步排队0,我们知道我们已经将1标记为已访问,即节点 - > V = 1,那么为什么他们的节点无法保留此信息,这是否必须使用指针进行操作?或者某处的信息丢失了?
这是我的结构。
struct hack{
char node;
struct hack *next;
struct hack *down;
int V; //we mark it 1 for visited and 0 is by defaulreat
};
任何帮助为什么节点无法保留它的V
的信息是否必须用指针做某事?