邻接矩阵
typedef char VertexType;
typedef int EdgeType;
#define MAXVEX 100
#define INFINITY 65536
typedef struct
{
VertexType vexs[MAXVEX];
EdgType arc[MAXVEX][MAXVEX];
int numVertexes,numEdges;
}MGraph;
------------------------------
void CreateMGraph(MGraph *G)
{
int i,j,k,w;
printf("输入顶点数和边数"\n");
scanf("%d,%d",&G->numVertexes,&G->numEdges); //输入顶点和边数
for(i=0;i<G->numVertexes;i++) // 读入顶点
scanf(&G->vexs[i])
for(i=0;i<G->numVertexes;i++)
for(j=0;j<G->numVertexes;j++)
G->arc[i][j]=INFINITY; //邻接矩阵初始化
for(k=0;k<G->numEdges;k++)
{
printf("输入边(v_i,v_j)上的下标i,下标j和权w:\n");
scanf("%d,%d,%d",&i,&j,&w)
G->arc[i][j]=w;
G->arc[j][i]=G->arc[i][j];
}
}
领接表
typedef int Status;
typedef char VertexType;
typedef int EdgeType;
typedef struct EdgeNode
{
int adjvex;
EdgeType info;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numNodes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *G)
{
int i,j,k;
EdgeNode *e;
printf("输入顶点数和边数:\n");
scanf("%d,%d",&G->numNodes,&G->numEdges);
for(i = 0;i < G->numNodes;i++)
{
scanf(&G->adjList[i].data);
G->adjList[i].firstedge=NULL;
}
for(k = 0;k < G->numEdges;k++)
{
printf("输入边(vi,vj)上的顶点序号:\n");
scanf("%d,%d",&i,&j);
e=(EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex=j;
e->next=G->adjList[i].firstedge;
G->adjList[i].firstedge=e;
e=(EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex=i;
e->next=G->adjList[j].firstedge;
G->adjList[j].firstedge=e;
}
}