标签:
头文件graph.h
#ifndef _GRAPH_H_ #define _GRAPH_H_ #define MAX_VER 100 #define ENDLESS 65535 typedef char VertexType; typedef int EdgeType; typedef struct _Graph { VertexType ver[MAX_VER]; EdgeType edge[MAX_VER][MAX_VER]; int num_ver,num_edge; }Graph,*pGraph; int locate(pGraph pg,char ch); void creat_array_graph(pGraph pg); void print_graph(Graph g); #endif
/**************************** 文件名:/graph.c 时间:2014.12.28 作者:XIAO_PING_PING 运行环境:DEV-C++ 4.9.9.2 内容:图的邻接矩阵存储数据结构 功能:自己写数据结构 *****************************/ #include <string.h> #include <stdlib.h> #include "graph.h" int locate(pGraph pg,char ch) { int i = 0; while(ch != pg->ver[i]) { i++; } return i; } void creat_array_graph(pGraph pg) { int i,j; char cout,cin; int num_out,num_in; int weight; printf("输入顶点数:"); scanf("%d",&pg->num_ver); printf("输入边数:"); scanf("%d",&pg->num_edge); printf("输入顶点数据:"); for(i = 0;i < pg->num_ver;i++) { pg->ver[i] = getchar(); while('\n' == pg->ver[i]) { pg->ver[i] = getchar(); } printf("点%d:%c\n ",i,pg->ver[i]); } for(i = 0;i < pg->num_ver;i++) { for(j = 0;j < pg->num_ver;j++) { pg->edge[i][j] = ENDLESS; } } for(i = 0;i < pg->num_edge;i++) { printf("输入要连接的顶点:"); cout = getchar(); while('\n' == cout) { cout = getchar(); } cin = getchar(); while('\n' == cin) { cin = getchar(); } printf("权重:"); scanf("%d",&weight); num_out = locate(pg,cout); num_in = locate(pg,cin); pg->edge[num_out][num_in] = weight; pg->edge[num_in][num_out] = weight; } } void print_graph(Graph g) { int i, j; for(i = 0; i < g.num_ver; i++) { for(j = 0; j < g.num_ver; j++) { printf("%d ", g.edge[i][j]); } printf("\n"); } }
#include <string.h> #include <stdlib.h> #include "graph.h" int main() { Graph gph; creat_array_graph(&gph); print_graph(gph); getch(); return 0; }
标签:
原文地址:http://blog.csdn.net/xiao_ping_ping/article/details/42214117