#include <stdio.h>
#define MAX_VERTEX_NUM 20
#define INFINITY 32767
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0
#define TRUE 1
#define FALSE 0
#define OUTPLACE -1
#define DG 1
#define DN 2
#define UDG 3
#define UDN 4
typedef int VRType;
typedef int Status;
typedef int VertexType;
typedef int InfoType;
typedef struct ArcCell
{
VRType adj;
InfoType info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
int kind;
}MGraph;
int LocateVex(MGraph *G,VertexType v)
{
int i;
for(i=0;i<MAX_VERTEX_NUM;i++)
if(v==G->vexs[i])
return i;
return OUTPLACE;
}
Status CreateGraph(MGraph *G)
{
printf("=========Create Graph==========\n");
printf("1----DG\n");
printf("2----DN\n");
printf("3----UDG\n");
printf("4----UDN\n");
printf("Please input the kind of the graph:");
scanf("%d",&G->kind);
switch(G->kind)
{
case DG: return CreateDG(G);
case DN: return CreateDN(G);
case UDG: return CreateUDG(G);
/* case UDN: return CreateUDN(G); */
default: return ERROR;
}
}
Status CreateDG(MGraph *G)
{
int i,j,k;
VertexType v1,v2;
printf("Please input the G.vexnum:");
scanf("%d",&G->vexnum);
printf("Please input the G.arcnum:");
scanf("%d",&G->arcnum);
for(i=0;i<G->vexnum;i++)
{
printf("Please input the vertex: ");
scanf("%d",&G->vexs[i]);
}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{ G->arcs[i][j].adj=0;
G->arcs[i][j].info=0;}
for(k=0;k<G->arcnum;k++)
{
printf("Please input the arc(v1,v2):");
scanf("%d,%d",&v1,&v2);
i=LocateVex(G,v1); j=LocateVex(G,v2);
if(i<0||j<0||i==j)
{ printf("WRONG!!!\n");
return ERROR;
}
G->arcs[i][j].adj=1;
}
return OK;
}
Status CreateDN(MGraph *G)
{
int i,j,k,w;
VertexType v1,v2;
printf("Please input the G.vexnum:");
scanf("%d",&G->vexnum);
printf("Please input the G.arcnum:");
scanf("%d",&G->arcnum);
for(i=0;i<G->vexnum;i++)
{
printf("Please input the vertex: ");
scanf("%d",&G->vexs[i]);
}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{ G->arcs[i][j].adj=0;
G->arcs[i][j].info=INFINITY;}
for(k=0;k<G->arcnum;k++)
{
printf("Please input the arc and the weight(v1,v2,w):");
scanf("%d,%d,%d",&v1,&v2,&w);
i=LocateVex(G,v1); j=LocateVex(G,v2);
if(i<0||j<0||i==j)
{ printf("WRONG!!!\n");
return ERROR;
}
G->arcs[i][j].adj=1;
G->arcs[i][j].info=w;
}
return OK;
}
Status CreateUDG(MGraph *G)
{
int i,j,k;
VertexType v1,v2;
printf("Please input the G.vexnum:");
scanf("%d",&G->vexnum);
printf("Please input the G.arcnum:");
scanf("%d",&G->arcnum);
for(i=0;i<G->vexnum;i++)
{
printf("Please input the vertex: ");
scanf("%d",&G->vexs[i]);
}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{
G->arcs[i][j].adj=0;
G->arcs[i][j].info=0;
}
for(k=0;k<G->arcnum;k++)
{
printf("Please input the arc(v1,v2):");
scanf("%d,%d",&v1,&v2);
i=LocateVex(G,v1); j=LocateVex(G,v2);
if(i<0||j<0||i==j)
{
printf("WRONG!!!\n");
return ERROR;
}
G->arcs[i][j].adj=1;
G->arcs[j][i]=G->arcs[i][j];
}
return OK;
}
/*Status CreateUDN(MGraph *G)
{
int i,j,k,w;
VertexType v1,v2;
printf("Please input the G.vexnum:");
scanf("%d",&G->vexnum);
printf("Please input the G.arcnum:");
scanf("%d",&G->arcnum);
for(i=0;i<G->vexnum;i++)
{
printf("Please input the vertex: ");
scanf("%d",&G->vexs[i]);
}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{
G->arcs[i][j].adj=0;
G->arcs[i][j].info=INFINITY;
}
for(k=0;k<G->arcnum;k++)
{
printf("Please input the arc and the weight(v1,v2,w):");
scanf("%d,%d,%d",&v1,&v2,&w);
i=LocateVex(G,v1); j=LocateVex(G,v2);
if(i<0||j<0||i==j)
{
printf("WRONG!!!\n");
return ERROR;
}
G->arcs[i][j].adj=1;
G->arcs[i][j].info=w;
G->arcs[j][i]=G->arcs[i][j];
}
return OK;
} */
Status PrintGraph(MGraph *G)
{
switch(G->kind)
{
case DG: return PrintDG(G);
case DN: return PrintDN(G);
case UDG: return PrintUDG(G);
/*case UDN: return PrintUDN(G);*/
default: return ERROR;
}
}
Status PrintDG(MGraph *G)
{
int i,j;
printf("========Print DG========\n");
for(i=0;i<G->vexnum;i++)
printf("%3d",G->vexs[i]);
printf("\n");
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
if(G->arcs[i][j].adj==1)
printf("%d-->%d ",G->vexs[i],G->vexs[j]);
printf("\n");
return OK;
}
Status PrintDN(MGraph *G)
{
int i,j;
printf("========Print DN========\n");
for(i=0;i<G->vexnum;i++)
printf("%3d",G->vexs[i]);
printf("\n");
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
if(G->arcs[i][j].adj==1)
printf("%d-->%d(%d) ",G->vexs[i],G->vexs[j],G->arcs[i][j].info);
printf("\n");
return OK;
}
Status PrintUDG(MGraph *G)
{
int i,j;
printf("========Print UDG========\n");
for(i=0;i<G->vexnum;i++)
printf("%3d",G->vexs[i]);
printf("\n");
for(i=0;i<G->vexnum;i++)
for(j=i;j<G->vexnum;j++)
if(G->arcs[i][j].adj==1)
printf("%d---%d ",G->vexs[i],G->vexs[j]);
printf("\n");
return OK;
}
/*Status PrintUDN(MGraph *G)
{
int i,j;
printf("========Print UDN========\n");
for(i=0;i<G->vexnum;i++)
printf("%3d",G->vexs[i]);
printf("\n");
for(i=0;i<G->vexnum;i++)
for(j=i;j<G->vexnum;j++)
if(G->arcs[i][j].adj==1)
printf("%d---%d(%d) ",G->vexs[i],G->vexs[j],G->arcs[i][j].info);
printf("\n");
return OK;
} */
main()
{
MGraph *G=NULL;
clrscr();
CreateGraph(G);
PrintGraph(G);
getch();
}
这个是创建各种图并且输出的程序,注释掉的部分是建立无向网的,不注释的话输结点的时候就乱套,求高手帮忙看看是怎么回事。