#include <stdio.h>
#include <stdlib.h>
//無向圖的鄰接矩陣存儲 by sixer 2009,9.1
/*
1.使用多個getchar()的時候應該考慮Enter鍵的處理
2.多個scanf輸入字元的時候也要注意Enter鍵 ,第二個scanf會讀取第一個的回車符
*/
#define MaxVertextNum 10
typedef char VertexType;
typedef int EdgeType;
typedef struct
{
VertexType vertexs[MaxVertextNum];
EdgeType edges[MaxVertextNum][MaxVertextNum];
int v,e;
}MGraph;
void createGraph(MGraph *G)
{
printf("PLZ input the num of Vertes and Edges like this(5,2):");
scanf("%d,%d",&(G->v),&(G->e));//使用者輸入頂點和邊數
int i;
//開始初始頂點清單
for(i=0;i<(G->v);i++)
{ getchar();
printf("PLZ input the mark of the Verte:");
scanf("%c",&(G->vertexs[i]));
}
//開始初始化邊清單
int j,k;
for(j=0;j<(G->v);j++)
for(k=0;k<(G->v);k++)
{
G->edges[j][k]=0;
}
for(i=0;i<(G->e);i++)
{
getchar();
char v1,v2;
printf("PLZ input the two vertexs of the edge:");
scanf("%c,%c",&v1,&v2);
//printf("Ur input edge is:%c,%c",v1,v2);
for(j=0;v1!=G->vertexs[j];j++);
for(k=0;v2!=G->vertexs[k];k++);
G->edges[j][k]=1;//如果為網則賦予使用者填入的權值
G->edges[k][j]=1;//如果為有向圖則去掉該句
}
getchar();//eat the absolutely last "Enter"
//開始初始化邊清單
}
void printGraph(MGraph *G)
{
int i,j;
printf("All Vertexs :");
for(i=0;i<G->v;i++)
printf(" %c",G->vertexs[i]);
printf("/nArray:/n");
for(i=0;i<G->v;i++)
for(j=0;j<G->v;j++)
{
printf(" %d ",G->edges[i][j]);
if(j==G->v-1)
{
printf("/n");
}
}
}
main()
{
MGraph *myG;
myG=malloc(sizeof(MGraph));//malloc return "void*" which means undefined pointer
createGraph(myG);
printGraph(myG);
getchar();
}