邻接表转换为逆邻接表

typedef struct ArcNode{
	int adjvex;
	int weight;
	ArcNode *next;
}ArcNode;
 
typedef struct VertexNode{
	int vertex;
	ArcNode *firstarc;
}VertexNode,AdjList[100];
 
typedef struct GraphAdjList{
	AdjList adjlist;
	int vexnum;
	int arcnum;
}GraphAdjList;

void transOutToIn(GraphAdjList gOut,GraphAdjList &gIn){
	
	gIn.vexnum =  gOut.vexnum; //初始化逆邻接表顶点数目   
	gIn.arcnum =  gOut.arcnum; //初始化逆邻接表边数目  
	
	for(int i = 0; i < gOut.vexnum; i++){
		gIn.adjlist[i].vertex = gOut.adjlist[i].vertex;//构造逆邻接表顶点向量 
		gIn.adjlist[i].firstarc = NULL;
	}
	
	for(int i = 0; i < gOut.vexnum; i++){
		ArcNode *p,*s;
		int j;
		
		p = gOut.adjlist[i].firstarc;//取出邻接表中第i个顶点的指向邻接表的指针   
		
		while(p){  //遍历邻接表中第i个顶点所有邻接边 
			j = p->adjvex;  
			s = new ArcNode;
			s->adjvex = i;//将 i 挂到 gIn.adjlist[j]上
			s->weight = p->weight;
			s->next = gIn.adjlist[j].firstarc; //将 i 挂到 j上 
			gIn.adjlist[j].firstarc = s;
			p = p->next;
		}
	}
	
 
}


上一篇:mac vscode could not import /github.com/gin-gonic/gin


下一篇:初识gin框架