//MGraph.h
#ifndef _MGRAPH_H_
#define _MGRAPH_H_
typedef void MGraph;
typedef void MVertex;
typedef void (MGraph_Printf)(MVertex*);
MGraph* MGraph_Create(MVertex** v,int n);
void MGraph_Destroy(MGraph* graph);
void MGraph_Clear(MGraph* graph);
int MGraph_AddEdge(MGraph* graph,int v1,int v2,int w);
int MGraph_RemoveEdge(MGraph* graph,int v1,int v2);
int MGraph_GetEdge(MGraph* graph,int v1,int v2);
int MGraph_TD(MGraph* graph,int v);
int MGraph_VertexCount(MGraph* graph);
int MGraph_EdgeCount(MGraph* graph);
void MGraph_Display(MGraph* graph,MGraph_Printf* pFunc);
#endif
//MGraph.c
#include "MGraph.h"
#include <malloc.h>
#include <stdio.h>
typedef struct _tag_MGraph
{
int count;
MVertex** v;
int** matrix;
}TMGraph;
MGraph* MGraph_Create(MVertex** v,int 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*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)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
ret = (tGraph!=NULL) && (v1>=0) && (v1<tGraph->count);
ret = ret && (v2>=0) && (v2<tGraph->count);
ret = ret && (w>=0);
if(ret)
{
tGraph->matrix[v1][v2] = w;
}
return ret;
}
int MGraph_RemoveEdge(MGraph* graph,int v1,int v2)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(v2<tGraph->count))
{
ret = tGraph->matrix[v1][v2];
tGraph->matrix[v1][v2] = 0;
}
return ret;
}
int MGraph_GetEdge(MGraph* graph,int v1,int v2)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(v2<tGraph->count))
{
ret = tGraph->matrix[v1][v2];
//tGraph->matrix[v1][v2] = 0;
}
return ret;
}
int MGraph_TD(MGraph* graph,int v)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
if((tGraph!=NULL)&&(v>=0)&&(v<tGraph->count))
{
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)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
if(tGraph!=NULL)
{
ret = tGraph->count;
}
return ret;
}
int MGraph_EdgeCount(MGraph* graph)
{
int ret = 0;
TMGraph* tGraph = (TMGraph*)graph;
if(graph!=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)
{
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("< ");
printf(tGraph->v[i]); //Vertex 1
printf(",");
printf(tGraph->v[j]); //Vertex 2
printf(", %d",tGraph->matrix[i][j]);
printf(" >");
printf(" ");
}
}
}
printf("\n");
}
}
//main.c
/*---------------------
*time:2016-4-13
----------------------*/
#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"}; //Ö¸ÕëÊý×飬[]µÄÓÅÏȼ¶±È*¸ß£¬Ö¸ÕëÊý×éµÄÿ¸öÔªËØΪָÕëÀàÐÍ£¬Í¨³£À´´æ·Å×Ö·û´®
//MVertex** v = {"A","B","C","D","E","F"}; //²»ÖªµÀΪɶ²»ÐÐ £»Ö»ÄܰѱäÁ¿Ãû£¨µØÖ·£©¸³¸øÖ¸Õ룬²»Äܰѳ£Á¿¸³¸øÖ¸Õë
MGraph* graph = MGraph_Create(v,6);
MGraph_AddEdge(graph,0,1,1);
MGraph_AddEdge(graph,1,0,1);
printf("A Degree : %d\n",MGraph_TD(graph,0));
printf("Vertex Count is : %d\n",MGraph_VertexCount(graph));
printf("Edge Count : %d\n",MGraph_EdgeCount(graph));
MGraph_Display(graph,print_data);
MGraph_Destroy(graph);
return 0;
}
1