听了国嵌唐老师的课,用C语言实现如下:
MGraph.c
#include <stdlib.h>
#include <stdio.h>
#include "MGraph.h"
typedef struct _tag_MGraph
{
int count;
MVertex** v;
int ** matrix;
}TMGraph;
MGraph* MGraph_Create(MVertex** v, int n) // O(n)
{
TMGraph* ret = NULL;
if( (v != NULL ) && (n > 0) )
{
ret = (TMGraph*)malloc(sizeof(TMGraph));
if( ret != NULL )
{
int* p = NULL;
ret->count = n;
ret->v = (MVertex**)malloc(sizeof(MVertex*) * n);
ret->matrix = (int**)malloc(sizeof(int*) * n);
p = (int*)calloc(n * n, sizeof(int));
if( (ret->v != NULL) && (ret->matrix != NULL) && (p != NULL) )
{
int i = 0;
for(i=0; i<n; i++)
{
ret->v[i] = v[i];
ret->matrix[i] = p + i * n;
}
}
else
{
free(p);
free(ret->matrix);
free(ret->v);
free(ret);
ret = NULL;
}
}
}
return ret;
}
void MGraph_Destroy(MGraph* graph)
{
TMGraph* tGraph = (TMGraph*)graph;
if(tGraph!=NULL)
{
free(tGraph->v);
free(tGraph->matrix[0]);
free(tGraph->matrix);
free(tGraph);
}
}
void MGraph_Clear(MGraph* graph)
{
TMGraph* tGraph = (TMGraph*)graph;
if(tGraph!=NULL)
{
int i = 0;
int j = 0;
for(i=0;i<tGraph->count;i++)
{
for(j=0;j<tGraph->count;j++)
{
tGraph->matrix[i][j] = 0;
}
}
}
}
int MGraph_AddEdge(MGraph* graph, int v1, int v2, int w) // O(1)
{
TMGraph* tGraph = (TMGraph*)graph;
int ret = (tGraph != NULL);
ret = ret && (0 <= v1) && (v1 < tGraph->count);
ret = ret && (0 <= v2) && (v2 < tGraph->count);
ret = ret && (0 <= w);
if( ret )
{
tGraph->matrix[v1][v2] = w;
}
return ret;
}
int MGraph_RemoveEdge(MGraph* graph,int v1,int v2)
{
int ret = MGraph_GetEdge(graph,v1,v2);
if(ret!=0)
{
((TMGraph*)graph)->matrix[v1][v2] = 0;
}
return ret;
}
int MGraph_GetEdge(MGraph* graph,int v1,int v2)
{
TMGraph* tGraph = (TMGraph*)graph;
int condition = (tGraph!=NULL);
int ret = 0;
condition = condition&&(0<=v1)&&(v1<tGraph->count);
condition = condition&&(0<=v2)&&(v2<tGraph->count);
if(condition)
{
ret = tGraph->matrix[v1][v2];
}
return ret;
}
int MGraph_TD(MGraph* graph,int v)
{
TMGraph* tGraph = (TMGraph*)graph;
int condition = (tGraph!=NULL);
int ret = 0;
condition = condition&&(0<=v)&&(v<tGraph->count);
if(condition)
{
int i = 0;
for(i=0;i<tGraph->count;i++)
{
if(tGraph->matrix[v][i]!=0)
{
ret++;
}
if(tGraph->matrix[i][v]!=0)
{
ret++;
}
}
}
return ret;
}
int MGraph_VertexCount(MGraph* graph)
{
TMGraph* tGraph = (TMGraph*)graph;
int ret = 0;
if(tGraph!=NULL)
{
ret = tGraph->count;
}
return ret;
}
int MGraph_EdgeCount(MGraph* graph)
{
TMGraph* tGraph = (TMGraph*)graph;
int ret = 0;
if(tGraph!=NULL)
{
int i = 0;
int j = 0;
for(i=0;i<tGraph->count;i++)
{
for(j=0;j<tGraph->count;j++)
{
if(tGraph->matrix[i][j] !=0)
{
ret++;
}
}
}
}
return ret;
}
void MGraph_Display(MGraph* graph, MGraph_Printf* pFunc) // O(n*n)
{
TMGraph* tGraph = (TMGraph*)graph;
if( (tGraph != NULL) && (pFunc != NULL) )
{
int i = 0;
int j = 0;
for(i=0; i<tGraph->count; i++)
{
printf("%d:", i);
pFunc(tGraph->v[i]);
printf(" ");
}
printf("\n");
for(i=0; i<tGraph->count; i++)
{
for(j=0; j<tGraph->count; j++)
{
if( tGraph->matrix[i][j] != 0 )
{
printf("<");
pFunc(tGraph->v[i]);
printf(", ");
pFunc(tGraph->v[j]);
printf(", %d", tGraph->matrix[i][j]);
printf(">");
printf(" ");
}
}
}
printf("\n");
}
}
MGraph.h
#ifndef _MGRAPH_H_
#define _MGRAPH_H_
typedef void MGraph;
typedef void MVertex;
typedef void (MGraph_Printf)(MVertex*);
/*创建并返回有n个顶点的图*/
MGraph* MGraph_Create(MVertex** v,int n);
/*销毁graph所指向的图*/
void MGraph_Destroy(MGraph* graph);
/*将graph所指向图的边集合清空*/
void MGraph_Clear(MGraph* graph);
/*在graph所指图中的v1和v2之间加上边,且边的权为w*/
int MGraph_AddEdge(MGraph* graph,int v1,int v2,int w);
/*在graph所指图中的v1和v2之间的边删除,返回权值*/
int MGraph_RemoveEdge(MGraph* graph,int v1,int v2);
/*在graph所指图中的v1和v2之间的边的权值返回*/
int MGraph_GetEdge(MGraph* graph,int v1,int v2);
/*在graph所指图中v顶点的度数返回*/
int MGraph_TD(MGraph* graph,int v);
/*在graph所指图中的顶点返回*/
int MGraph_VertexCount(MGraph* graph);
/*在graph所指图中的顶点返回*/
int MGraph_EdgeCount(MGraph* graph);
void MGraph_Display(MGraph* graph,MGraph_Printf* pFunc);
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
#include "MGraph.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void print_data(MVertex* v)
{
printf("%s", (char*)v);
}
int main(int argc, char *argv[])
{
MVertex* v[] = {"A", "B", "C", "D", "E", "F"};
MGraph* graph = MGraph_Create(v, 5);
MGraph_AddEdge(graph,0,1,1);
MGraph_AddEdge(graph,1,2,1);
MGraph_AddEdge(graph,2,3,1);
MGraph_AddEdge(graph,2,4,1);
MGraph_AddEdge(graph,3,4,9);
printf("Weight <D,E>: %d\n",MGraph_GetEdge(graph,3,4));
printf("Vertex Count: %d\n",MGraph_VertexCount(graph));
printf("Edge Count: %d\n",MGraph_EdgeCount(graph));
printf("Degree C: %d\n",MGraph_TD(graph,2));
MGraph_Display(graph, print_data);
MGraph_Clear(graph);
printf("After clear...\n");
printf("Weight <D,E>: %d\n",MGraph_GetEdge(graph,3,4));
printf("Vertex Count: %d\n",MGraph_VertexCount(graph));
printf("Edge Count: %d\n",MGraph_EdgeCount(graph));
printf("Degree C: %d\n",MGraph_TD(graph,2));
MGraph_Destroy(graph);
return 0;
}
运行效果: