Help with adjacency list

Hello i am trying to remove all arcs with a node(from an adjacency list) using the refresh funtion. i am getting a segmentation fault. can someone please help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void refresh(list<int> &a,int node)
{
	//adj is the global adjacency list
	list<int>::iterator it;
	bool flag;
	for(int j=0;j<TOTAL;j++)//TOTAL is number of nodes
	{
		flag=false;
		for(it=adj[j].begin();it!=adj[j].end();++it)
		{
			int aa=(*it);//just added to help me on debugging
			if( (*it)==node )
			{
				if(adj[j].size()==1){adj[j].clear();flag=true;break;}//i thought that maybe that was the problem when it was the last element and then it went to ++it
				adj[j].erase(it);flag=true;}

			else if(   ( (*it)!=node )&&(visited[(*it)-1]==0)   )
			{flag=false;
			break;}
			if(adj[j].empty()){break;}
		}
		if(flag){ a.push_back(j+1);}
	}
}
I guess that adj[j].erase(it); is invalidating the iterator.

Use it = adj[j].erase(it); instead (don't increment the iterator in that case)
Last edited on
Topic archived. No new replies allowed.