Hello anduril462 .Thanks a lot till now. I tried the above template to implement in my code but something is really messing up things.
Firstly I have initialized a member in mainList called int visited. But i write int visited = F, my code goes all the way full with error saying that pNext is not a member of mainList.
Other Issue is that in BFS it doesn't like to get two variables with the same name start that I changed accordingly .
Also, it doesnt like the first value of the Delete_queue. I tried changing it too...but still deosnt work.
coz it needs a pointer to int valu and not struct i guess. I tried changing it to
queue = Delete_Queue(&search->nodeNum, queue);
and the overall error i get is Segmentation error. The code I was trying is
Code:
void BFS(struct mainList *nodes, int start1, int goal)
{
struct mainList *start;
struct mainList *search;
struct Q *queue;
// returns a pointer to the mainList node with nodeNum start
start1 = find_node(nodes, start1);
struct mainList *find_node(struct mainList *list, int value)
{
// loop through list looking for a node with nodeNum equal to value
// if you find it, return a pointer to that node, otherwise return NULL
//list = *pHead;
while(list != NULL)
{
if(value == list->nodeNum)
{
list= list->pNext;
}
else
return 0;
}
}
/* if (!start) {
// bail out
}
*/
queue = Insert_Queue( start1, queue);
while (queue) {
// extract next node to search
queue = Delete_Queue(&search->nodeNum, queue);
if (search->nodeNum == goal) {
printf("we found out Goal\n");
printf("%d", goal);
return ;
// we found our node, act accordingly
}
else {
// set visited to true for search
search->visited = T;
insert_unvisited_children(queue, search, nodes);
struct Q *insert_unvisited_children(struct Q *queue, struct mainList *search, struct mainList *nodes)
{
struct adjList *adj_node;
struct mainList *main_node;
for (adj_node = search->pAdjListHead; adj_node; adj_node = adj_node->pNext) {
// find the mainList node for this adjList node
main_node = find_node(nodes, adj_node->nodeNum);
// if main_node has not been visited, insert it in queue
if(main_node->visited = F )
queue = Insert_Queue(main_node->nodeNum, queue);
}
return queue;
}
}
}
}
the coloured codes in the BFS code are obviously written separately. I wrote in BFS just fro posting it to the forum .
Please have a look. I am stuck at the very last step now.