有关图的存储
#include<stdio.h>#include<stdlib.h>
//邻接矩阵存储
#define Max 30
typedef struct
{
int n,e;//顶点数和边数
char vexs[Max];//顶点数组
int edges[Max][Max];//邻接矩阵
int kind;
}MGraph;
void CreateUDG(MGraph *G)
{
int i,j,k,w;
printf("输入边数和顶点数\n");
scanf("%d%d",&G->e,&G->n);//输入边数和顶点数
printf("依次输入顶点值\n");
for(i=0;i<G->n;i++)
G->vexs[i] = getchar();
for(i=0;i<G->n;i++)
for(j = 0;j<G->n;j++)
G->edges[i][j] = 0;
printf("依次输入图的顶点和权值\n");
for(k = 0;k<G->e;k++)
{
scanf("%d%d%d",&i,&j,&w);//通过判断是否有权决定这两个点是否相关
G->edges[i][j] = w;
G->edges[j][i] = w;
}
}
int Locatevex(MGraph *G,char v)
{
int j = 0;
int k;
for(k=0;k<G->n;k++)
if(G->vexs[k] == v)
{
j = k;
break;
}
return j;
}
void CreateDG(MGraph *G)
{
int i,j,k,w;
char v1,v2;
printf("输入边数和顶点数\n");
scanf("%d%d",&G->e,&G->n);
for(i=0;i<G->n;i++)
for(j=0;j<G->n;j++)
G->edges[i][j] = 0;
printf("依次输入顶点值\n");
for(i = 0;i<G->n;i++)
scanf("%c",&G->vexs[i]);
printf("依次输入图的顶点和权值\n");
for(k = 0;k<G->n;k++)
{
scanf("%d%d%d",&v1,&v2,&w);
i = Locatevex(G,v1);
j = Locatevex(G,v2);
G->edges[i][j] = w;
}
}
void CreateGraph(MGraph &G)
{
printf("请输入您要创建的图的种类:1:有向;2:无向\n");
scanf("%d",&G.kind);
switch(G.kind)
{
case 1: CreateDG(&G);break;
case 2: CreateUDG(&G);break;
default: printf("请重新输入:\n");
}
}
/*
//邻接表存储
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
char data;
ArcNode *firstarc;
}VNode;
typedef struct
{
VNode adjlist[Max];
int n,e;
}ALGraph;
typedef VNode adjlist[Max];//有点小不明白这个结构是如何的????
void CreateGraph(ALGraph &G)
{
int i,j,e;//e累计边数
char v1,v2;
ArcNode *p;
printf("输入边数\n");
scanf("%d",&G->n);
printf("依次输入顶点值\n");
for(i=0;i<n;i++)
{
scanf("%c",&G->adjlist[i].data);
G->adjlist[i].nextarc = NULL;
}
e = 0;
scanf("%c%c",&v1,&v2);
i = Locatevex(G,v1);
j = Locatevex(G,v2);
while(i>0)//无向图没一次可有2个表的信息存储到相关位置
{
e++;
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = G->adjlist[i].firstarc;
G->adjlst[i].firstarc = p;
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = i;
p->nextarc = G->adjlist[j].firstarc;
G->adjlst[j].firstarc = p;
scanf("%c%c",&v1,&v2);
i = Locatevex(G,v1);
j = Locatevex(G,v2);
}
G->n = e;
}
*/
int main()
{
MGraph *G;
CreateGraph(G);//程序运行时说这里传参不对?????请各位指点下
system("pause");
return 0;
}