Reputation: 23
// Recur for all the vertices adjacent to current vertex
list<int>::iterator i;
for (i = adj[src].begin(); i != adj[src].end(); ++i)
if (!visited[*i])
// Do Something
How do I write this code in C? I tried the following, is it correct?
while (graph->adjLists[src]->next != NULL)
{
int i = graph->adjLists[src]->vertex;
if (!visited[i])
{
// Do Something
}
graph->adjLists[src] = graph->adjLists[src]->next;
Upvotes: 0
Views: 51
Reputation: 596397
Close, but no, it is not correct. It would need to look more like this:
node *current = graph->adjLists[src]; // start at the head of the list
while (current != NULL)
{
if (!visited[current->vertex])
{
// Do Something
}
current = current->next;
}
Obviously, replacing node
with whatever the real type of your list nodes is.
Upvotes: 0
Reputation: 87959
Just comparing the two you can see they're not the same. Look at the role of the i
variable for instance. Probably something like this, using a pointer instead of an iterator
XXXX* i = graph->adjLists[src];
while (i != NULL)
{
if (!visited[i->vertex])
{
// Do Something
}
i = i->next;
From the code you've posted I cannot say what XXXX
should be, but hopefully you can figure it out.
Upvotes: 1