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

데이터 구조 - 그림 - C 언어 - 인접 행렬
공식 표기 법
#include <stdio.h>
#include <stdlib.h>

#define MaxVertexNum 50

typedef int WeightType;
typedef int Vertex;
typedef int DataType;
/* */
typedef struct GNode *pGNode;
struct GNode {
	int nv;//  
	int ne;// 
	WeightType G[MaxVertexNum][MaxVertexNum];
	DataType data[MaxVertexNum];
};
typedef pGNode MGraph;

/* */
typedef struct ENode *pENode;
struct ENode {
	Vertex v, w;//   v->w
	WeightType weight;//  
};
typedef pENode Edge;

/*
*@program          
*@param vertexNum     
*@return graph      
*/
MGraph createGraph(int vertexNum) {
	Vertex v, w;
	MGraph graph;

	graph = (MGraph)malloc(sizeof(GNode));
	graph->ne = 0;
	graph->nv = vertexNum;

	for (v = 0; v < graph->nv; v++)
		for (w = 0; w < graph->nv; w++)
			graph->G[v][w] = 0;

	return graph;
}

void insertEdge(MGraph graph, Edge edge) {
	graph->G[edge->v][edge->w] = edge->weight;
}

MGraph buildGraph() {
	MGraph graph;
	int vertex;

	scanf("       %d", &vertex);
	graph = createGraph(vertex);
	scanf("      %d", &(graph->ne));//      !!!!!
	
	if (graph->ne != 0) {
		for (int i = 0; i < graph->ne; i++) {
			Edge edge = (Edge)malloc(sizeof(struct ENode));
			scanf("       %d", &edge->v);
			scanf("       %d", &edge->w);
			scanf("      %d", &edge->weight);
			insertEdge(graph, edge);
		}
	}

	for (int i = 0; i < vertex; i++) {
		scanf("%c", &(graph->data[i]));
	}
}

응시 표기 법
int g[MAX][MAX],nv,ne;
void createGraph(){
	int vertex;
	int v,w,weight;
	int ne;
	for(int i=0;v<vertex;v++)
		for(int j=0;w<vertex;w++)
			g[i][j]=0;
	scanf("%d",&ne);
	for(int i=0;i<ne;i++){
		scanf("%d %d %d",&v,&w,&weight);
		g[v][w]=weight;
	}	
}

좋은 웹페이지 즐겨찾기