

/*创建并返回n个顶点的图结构*/ Graph* graph_creat(int n); /*销毁图结构*/ void graph_destroy(Graph* graph); /*清空图结构*/ void graph_clear(Graph* graph); /*在图的顶点v1和v2之间增加权值为w的边*/ int graph_add_edge(Graph* graph, int v1, int v2, int w); /*删除顶点v1和v2的边,返回权值*/ int graph_remove_edge(Graph* graph, int v1, int v2); /*获得顶点v1和v2之间边的权值*/ int graph_get_edge(Graph* graph,int v1, int v2); /*返回顶点v的度*/ int graph_td(Graph* graph, int v); /*返回图的顶点的个数*/ int graph_vertex_count(Graph* graph); /*返回图中顶点的边数*/ int graph_edge_count(Graph* graph);

typedef struct {
int count;
m_vertex** v;
int** matrix;
}tm_graph;#include <stdio.h>
#include <malloc.h>
#include "graph.h"
/*头结点结构体*/
typedef struct {
int count;
m_vertex** v; //指向n个类型为(m_vertex *)的指针区域。该区域的指针指向记录结点信息的内存
int** matrix;
} tm_graph;
MGraph* graph_creat(m_vertex** v, int n)
{
tm_graph* ret = NULL;
if((v != NULL) && (n > 0))
{
ret = (tm_graph*)malloc( sizeof(tm_graph) );
if(ret != NULL)
{
int i = 0;
int *p = NULL;
ret->count = n;
ret->v = (m_vertex**)malloc(sizeof(m_vertex *) *n);
/*申请一维地址空间*/
ret->matrix = (int**)malloc(sizeof(int*) * n);
/*申请一维数据空间,使用calloc可以把邻接矩阵清空,初始化为0*/
//p = (int*)malloc(sizeof(int) * n * n);
p = (int *)calloc(n * n, sizeof(int));
if( (ret->v != NULL) && (ret->matrix != NULL) && (p != NULL))
{
for(i=0; i < n; i++)
{
/*保存指向顶点数据的指针*/
ret->v[i] = v[i];
/*将数据空间与地址空间相连接*/
ret->matrix[i] = (p + i * n);
}
}
else
{
free(ret->v);
free(ret->matrix);
free(p);
}
}
}
return ret;
}
void graph_destroy(MGraph* graph)
{
tm_graph* t_graph = (tm_graph*)graph;
if(t_graph != NULL)
{
free(t_graph->v);
/*注:以下两步释放的顺序不能换*/
free(t_graph->matrix[0]);
free(t_graph->matrix);
free(t_graph);
}
}
/*清空图结构*/
void graph_clear(MGraph* graph)
{
tm_graph* t_graph = (tm_graph*)graph;
if(t_graph != NULL)
{
int i = 0;
int j = 0;
for(i=0; i < t_graph->count; i++)
{
for(j=0; j < t_graph->count; j++)
{
t_graph->matrix[i][j] = 0;
}
}
}
}
/*在图的顶点v1和v2之间增加权值为w的边*/
int graph_add_edge(MGraph* graph, int v1, int v2, int w)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
ret = (t_graph != NULL) && (v1 >= 0) && (v1 < t_graph->count);
ret = (ret) && (v1 >= 0) && (v1 < t_graph->count);
ret = (ret) && (w >= 0);
if(ret)
{
t_graph->matrix[v1][v2] = w;
}
return ret;
}
/*删除顶点v1和v2的边,返回权值*/
int graph_remove_edge(MGraph* graph, int v1, int v2)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
ret = (t_graph != NULL) && (v1 > 0) && (v1 < t_graph->count);
ret = (ret) && (v1 > 0) && (v1 < t_graph->count);
if(ret)
{
ret = t_graph->matrix[v1][v2];
t_graph->matrix[v1][v2] = 0;
}
}
/*获得顶点v1和v2之间边的权值*/
int graph_get_edge(MGraph* graph,int v1, int v2)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
ret = (t_graph != NULL) && (v1 > 0) && (v1 < t_graph->count);
ret = (ret) && (v1 > 0) && (v1 < t_graph->count);
if(ret)
{
ret = t_graph->matrix[v1][v2];
}
return ret;
}
/*返回顶点v的度*/
int graph_td(MGraph* graph, int v)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
if(t_graph != NULL)
{
int i = 0;
/*出度*/
for(i=0; i<t_graph->count; i++)
{
if(t_graph->matrix[v][i] != 0)
{
ret++;
}
}
/*入度*/
for(i=0; i<t_graph->count; i++)
{
if(t_graph->matrix[i][v] != 0)
{
ret++;
}
}
}
return ret;
}
/*返回图的顶点的个数*/
int graph_vertex_count(MGraph* graph)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
if(t_graph != NULL)
{
ret = t_graph->count;
}
}
/*返回图中顶点的边数*/
int graph_edge_count(MGraph* graph)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
if(t_graph != NULL)
{
int i = 0;
int j = 0;
for(i=0; i < t_graph->count; i++)
{
for(j=0; j < t_graph->count; j++)
{
if(t_graph->matrix[i][j] != 0)
{
ret++;
}
}
}
}
return ret;
}
void graph_display(MGraph* graph, graph_printf* p_func)
{
tm_graph* t_graph = (tm_graph*)graph;
int ret = 0;
if((t_graph != NULL) && (p_func != NULL))
{
int i = 0;
int j = 0;
/*打印结点*/
for(i=0; i<t_graph->count; i++)
{
printf("%d", i);
printf(",");
p_func(t_graph->v[i]);
printf(" ");
}
/*打印边*/
for(i=0; i<t_graph->count; i++)
{
for(j=0; j<t_graph->count; j++)
{
if(t_graph->matrix[i][j] != 0)
{
printf("<");
p_func(t_graph->v[i]);
printf(", ");
p_func(t_graph->v[j]);
printf(", ");
printf("%d", t_graph->matrix[i][j]);
printf(">");
printf(" ");
}
}
}
}
}

#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"
#include "l_graph.h"
typedef void LGraph;
typedef void l_vertex;
typedef struct {
int count;
l_vertex** v;
LinkList** la;
}tl_graph;
typedef struct {
LinkListNode header;
int v;
int w;
}t_list_node;
/*创建并返回n个顶点的图结构*/
LGraph* graph_creat(l_vertex** v, int n)//O(n)
{
tl_graph* ret = NULL;
int ok = 1;
if((v != NULL) && (n > 0))
{
ret = (tl_graph*)malloc( sizeof(tl_graph) );
if(ret != NULL)
{
ret->count = n;
ret->v = (l_vertex**)calloc(n, sizeof(l_vertex*));
ret->la = (LinkList**)calloc(n, sizeof(LinkList*));
ok = (ret->v != NULL) && (ret->la != NULL);
if(ok)
{
int i = 0;
for(i=0; i<n; i++)
{
ret->v[i] = v[i];
}
/*注意此处对创建链表返回值的监测*/
for(i=0; (i<n) && ok ; i++)
{
ok = ok && ((ret->la[i] = LinkList_Create()) != NULL);
}
}
/*不成功分别对应两种情况
1、结点数据指针的空间或存储链表指针的指针数组的空间未申请成功
2、创建链表不成功*/
if(!ok)
{
if( ret->la != NULL)
{
int i=0;
for(i=0; i<n; i++)
{
LinkList_Destroy(ret->la[i]);
}
}
free(ret->la);
free(ret->v);
free(ret);
ret = NULL;
}
}
}
return ret;
}
/*销毁图结构*/
void graph_destroy(LGraph* graph)//O(n)
{
tl_graph* t_graph = (tl_graph*)graph;
if(t_graph != NULL)
{
int i = 0;
for(i=0; i<t_graph->count; i++)
{
LinkList_Destroy(t_graph->la[i]);
}
free(t_graph->la);
free(t_graph->v);
free(t_graph);
}
}
/*清空图结构*/
void graph_clear(LGraph* graph)
{
tl_graph* t_graph = (tl_graph*)graph;
if(t_graph != NULL)
{
int i = 0;
int j = 0;
for(i=0; i<t_graph->count; i++)
{
for(j=0; j<LinkList_Length(t_graph->la[i]); j++)
{
free(LinkList_Delete(t_graph->la[i], 0));
}
}
}
}
/*在图的顶点v1和v2之间增加权值为w的边*/
int graph_add_edge(LGraph* graph, int v1, int v2, int w)//O(1)
{
tl_graph* t_graph = (tl_graph*)graph;
t_list_node* node = NULL;
int ret = 0;
ret = (t_graph != NULL);
ret = (ret) && (0 <= v1) && (v1 < t_graph->count);
ret = (ret) && (0 <= v2) && (v2 < t_graph->count);
ret = (ret) && (0 < w);
ret = (ret) && (node = (t_list_node*)malloc(sizeof(t_list_node)));
if(ret)
{
node->v = v2;
node->w = w;
LinkList_Insert(t_graph->la[v1], (LinkListNode*)node, 0);
}
return ret;
}
/*删除顶点v1和v2的边,返回权值*/
int graph_remove_edge(LGraph* graph, int v1, int v2)//O(n*n)
{
tl_graph* t_graph = (tl_graph*)graph;
int ret = 0;
ret = (t_graph != NULL);
ret = (ret) && (0 <= v1) && (v1 < t_graph->count);
ret = (ret) && (0 <= v2) && (v2 < t_graph->count);
if(ret)
{
int i = 0;
t_list_node* node = NULL;
for(i=0; i<LinkList_Length(t_graph->la[v1]); i++)
{
node = (t_list_node*)LinkList_Get(t_graph->la[v1], i);
if(node->v == v2)
{
ret = node->w;
LinkList_Delete(t_graph->la[v1], i);
free(node);
break;
}
}
}
return ret;
}
/*获得顶点v1和v2之间边的权值*/
int graph_get_edge(LGraph* graph,int v1, int v2) //O(n*n)
{
tl_graph* t_graph = (tl_graph*)graph;
int ret = 0;
ret = (t_graph != NULL);
ret = (ret) && (0 <= v1) && (v1 < t_graph->count);
ret = (ret) && (0 <= v2) && (v2 < t_graph->count);
if(ret)
{
int i = 0;
t_list_node* node = NULL;
for(i=0; i<LinkList_Length(t_graph->la[v1]); i++)
{
node = (t_list_node*)LinkList_Get(t_graph->la[v1], i);
if(node->v == v2)
{
ret = node->w;
break;
}
}
}
}
/*返回顶点v的度*/
int graph_td(LGraph* graph, int v)//O(n*n*n)
{
tl_graph* t_graph = (tl_graph*)graph;
int ret = 0 ;
if((t_graph != NULL) && (0 <= v) && (v < t_graph->count))
{
int i = 0;
int j = 0;
t_list_node* node = NULL;
/*入度*/
for(i=0; i < t_graph->count; i++)
{
for(j=0; j < LinkList_Length(t_graph->la[i]); j++)
{
node = (t_list_node*)LinkList_Get(t_graph->la[i], j);
if(node->v == v)
{
ret++;
}
}
}
/*出度*/
for(i=0; i < LinkList_Length(t_graph->la[v]); i++)
{
ret++;
}
}
return ret;
}
/*返回图的顶点的个数*/
int graph_vertex_count(LGraph* graph)//O(1)
{
tl_graph* t_graph = (tl_graph*)graph;
int ret = 0 ;
if(t_graph != NULL)
{
ret = t_graph->count;
}
return ret;
}
/*返回图的边数*/
int graph_edge_count(LGraph* graph)//O(n)
{
tl_graph* t_graph = (tl_graph*) graph;
int ret = 0;
if(t_graph != NULL)
{
int i = 0;
int j = 0;
for(i=0; i < t_graph->count; i++)
{
ret += LinkList_Length(t_graph->la[i]);
}
}
return ret;
}
void graph_display(LGraph* graph, graph_printf* p_func)//O(n*n*n)
{
tl_graph* t_graph = (tl_graph*)graph;
int ret = 0;
if((t_graph != NULL) && (p_func != NULL))
{
int i = 0;
int j = 0;
t_list_node* node = NULL;
/*打印结点*/
for(i=0; i<t_graph->count; i++)
{
printf("%d", i);
printf(",");
p_func(t_graph->v[i]);
printf(" ");
}
printf("\n");
/*打印边*/
for(i=0; i<t_graph->count; i++)
{
for(j=0; j<LinkList_Length(t_graph->la[i]); j++)
{
node = (t_list_node*)LinkList_Get(t_graph->la[i], j);
printf("<");
p_func(t_graph->v[i]);
printf(", ");
p_func(t_graph->v[node->v]);
printf(", ");
printf("%d", node->w);
printf(">");
printf(" ");
}
}
printf("\n");
}
}


原文地址:http://blog.csdn.net/u011467781/article/details/45271689