1、邻接矩阵创建
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXVEX 100
#define GRAPH_INFINITY 65535
typedef int Status;
typedef char VertexType;
typedef int EdgeType;
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numNodes, numEdges;
}MGraph;
void CreateMGraph(MGraph* G)
{
int i, j, k, w;
VertexType vtx;
printf("输入顶点数和边数:\n");
scanf("%d,%d", &G->numNodes, &G->numEdges);
for (i = 0; i < G->numNodes; i++)
{
printf("请输入顶点数据:\n");
scanf("%c", &vtx);
getchar();
G->vexs[i] = vtx;
}
for (i = 0; i < G->numNodes; i++)
for (j = 0; j < G->numNodes; j++)
G->arc[i][j] = GRAPH_INFINITY;
for (k = 0; k < G->numEdges; k++)
{
printf("输入边(vi,vj)上的下标i,下标j和权w:\n");
scanf("%d,%d,%d", &i, &j, &w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];
}
}
int main(void)
{
MGraph G;
CreateMGraph(&G);
return 0;
}
2、邻接表创建
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
typedef char VertexType;
typedef int EdgeType;
#define MAXVEX 100
typedef struct EdgeNode
{
int adjvex;
int weight;
struct EdgeNode* next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
EdgeNode* firstEdge;
}VertexNode,VexList[MAXVEX];
typedef struct MapByAdjList
{
int vexNum, edgeNum;
VexList vexlist;
}MapByAdjList;
void CreateMapByAdjList(MapByAdjList* M)
{
int i, j, k, w;
printf("请输入图的顶点个数和边数:\n");
scanf("%d,%d", &M->vexNum, &M->edgeNum);
for (i = 0; i < M->vexNum; i++)
{
printf("请输入第%d个顶点的数据:\n", i + 1);
scanf("%c", &M->vexlist[i].data);
getchar();
M->vexlist[i].firstEdge = NULL;
}
for (k = 0; k < M->edgeNum; k++)
{
printf("请输入边(vi,vj)的下标以及边的权值w:\n");
scanf("%d,%d,%d", &i, &j,&w);
EdgeNode* newEdge=(EdgeNode*) malloc(sizeof(EdgeNode));
newEdge->next = M->vexlist[i].firstEdge;
newEdge->weight = w;
newEdge->adjvex = j;
M->vexlist[i].firstEdge = newEdge;
newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
newEdge->next = M->vexlist[j].firstEdge;
newEdge->weight = w;
newEdge->adjvex = i;
M->vexlist[j].firstEdge = newEdge;
}
}
int main()
{
MapByAdjList M;
CreateMapByAdjList(&M);
return 0;
}
3、十字链表法创建
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
typedef char VexType;
#define MAXVEX 100
typedef struct EdgeNode
{
int tailvex;
int headvex;
struct EdgeNode* headlink;
struct EdgeNode *taillink;
}EdgeNode;
typedef struct VertexNode
{
VexType data;
EdgeNode* firstin;
EdgeNode* firstout;
}VertexNode,VertexList[MAXVEX];
typedef struct CrossLinkMap
{
int vexNum, edgeNum;
VertexList vertexList;
}CrossLinkMap;
void CreateCrossLinkMap(CrossLinkMap* M)
{
int i=0, j=0, k=0;
printf("请输入顶点数量和边数量:\n");
scanf("%d,%d", &M->vexNum, &M->edgeNum);
for (i = 0; i < M->vexNum; i++)
{
printf("请输入第%d个顶点信息:\n",i+1);
scanf("%c", &M->vertexList[i].data);
getchar();
M->vertexList[i].firstin = NULL;
M->vertexList[i].firstout = NULL;
}
for (k= 0; k < M->edgeNum; k++)
{
printf("请输入边<vi,vj>的下标i,j:\n");
scanf("%d,%d", &i, &j);
EdgeNode* newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
newEdge->tailvex = i;
newEdge->headvex = j;
newEdge->headlink = M->vertexList[j].firstin;
newEdge->taillink = M->vertexList[i].firstout;
M->vertexList[i].firstout = newEdge;
M->vertexList[j].firstin = newEdge;
}
}
int main()
{
CrossLinkMap M;
CreateCrossLinkMap(&M);
printf("A firstout:%d %d\n", M.vertexList[0].firstout->tailvex, M.vertexList[0].firstout->headvex);
printf("A firstin:%d %d\n", M.vertexList[0].firstin->tailvex, M.vertexList[0].firstin->headvex);
printf("A firstin.headlink:%d %d\n", M.vertexList[0].firstin->headlink->tailvex, M.vertexList[0].firstin->headlink->headvex);
return 0;
}