데이터 구조 - 그림 - C 언어 - 인접 표

데이터 구조 - 그림 - C 언어 - 인접 표
#include 
#include 

#define Max 100

typedef int Vertex;
typedef int WeightType;

/**
*
* @author Caedios
* @attr v1   v1,    v1->v2
* @attr v2   v2,    v1->v2
* @attr weight     
*
*/
struct ENode {
	Vertex v1, v2;
	WeightType weight;
};
typedef struct ENode * pENode;

/**
*
* @author Caedios
* @attr subNum      
* @attr weight    
* @attr pVNode        
*
*/
struct VNode {
	Vertex subNum;
	WeightType weight;
	pVNode nextVNode;
};
typedef struct VNode * pVNode;

/**
*
* @author Caedios
* @attr headVNode          
*
*/
struct HeadVNode {
	pVNode headVNode;
};
typedef struct HeadVNode AdjList[Max];

/**
*
* @author Caedios
* @attr numVertex    
* @attr numEdge   
* @attr adjList    
*
*/
struct Graph {
	int numVertex;
	int numEdge;
	AdjList adjList;
};
typedef struct Graph * pGraph;

pGraph createGraph(int vertexNum) {
	Vertex v;
	pGraph graph;

	graph = (pGraph)malloc(sizeof(struct Graph));
	graph->numVertex = vertexNum;
	graph->numEdge = 0;

	for (v = 0; v < graph->numVertex; v++)
		graph->adjList[v].headVNode = NULL;

	return graph;
}

pGraph insertGraph(pGraph graph,pENode edge) {
	pVNode newNode = (pVNode)malloc(sizeof(struct VNode));
	newNode->subNum = edge->v2;
	newNode->weight = edge->weight;
	newNode->nextVNode = graph->adjList[edge->v1].headVNode;
	graph->adjList[edge->v1].headVNode = newNode;
	return graph;
}

pGraph buildGraph() {
	pGraph graph;
	pENode edge;

	int vertexNum;
	scanf("%d", &vertexNum);
	graph = createGraph(vertexNum);

	scanf("%d", graph->numEdge);
	if (graph->numEdge > 0) {
		edge = (pENode)malloc(sizeof(struct ENode));
		for (int i = 0; i < graph->numEdge; i++) {
			scanf("%d %d %d", edge->v1, edge->v2, edge->weight);
			insertGraph(graph, edge);
		}
	}
	return graph;
}

좋은 웹페이지 즐겨찾기