鄰接矩陣轉鄰接表

#include <iostream>
using namespace std;

const int inf = 99999999;
int arr[100][100];

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 graphArrToList(GraphAdjList &G){
	
	G.vexnum = n;
	G.arcnum = m;
	
	for(int i = 0; i < G.vexnum; i++){
		G.adjlist[i].vertex = i;
		G.adjlist[i].first = NULL;
	}
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if(arr[i]][j] != inf && arr[i]][j] != 0){
				ArcNode *p;
				p->adjvex = j;
				p->weight = arr[i][j];
				p->next = G.adjlist[i].firstarc; //頭插法 
				G.adjlist[i].firstarc = p;
			}
		}
	}
}



int main(){
	
	GraphAdjList G;
	createGraphAdjList(G);
	printfGraphAdjList(G);
	createGraphAdjArr(G);
	printfGraphAdjArr(G.vexnum);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章