//图的邻接表
#define MAXVERTEX 100
typedef char vertextype;// define the type of vertex
typedef int acrtype;//define the type of weight
typedef struct ArcNode //ArcNode
{
int adjvex;// adjacant vertex
acrtype weight;//
struct ArcNode *next;
}ArcNode;
typedef struct VertexNode //vertexnode
{
vertextype data;// store the information of vertex
ArcNode *firstarc;//the head point of Arc
}VertexNode,AdjList[MAXVERTEX];
typedef struct
{
AdjList adjlist;//define the adjlist
int numvertex;//the nunmber of vertex;
int numarc;//the number of arc;
}GraphAdjList;
void CreateAdjListGraph(GraphAdjList & G)
{
ArcNode *e;
cin >> G.numvertex;
cin >> G.numarc;
for (int i = 0;i < G.numvertex;i++)
{
cin >> G.adjlist[i].data;
G.adjlist[i].firstarc=NULL:
}
for (int k = 0;k < G.numarc;k++)
{
int i, j, w;
cin >> i >> j >> w;
e = new ArcNode;
e->adjvex = j;
e->weight = w;
e->next = G.adjlist[i].firstarc;
G.adjlist[i].firstarc = e;
e = new ArcNode;
e->adjvex = i;
e->weight = w;
e->next = G.adjlist[j].firstarc;
G.adjlist[j].firstarc = e;
}
}
void PrintfGraphAdjList(GraphAdjList G)
{
for (int i = 0;i < G.numvertex;i++)
{
ArcNode *p = G.adjlist[i].firstarc;
cout << G.adjlist[i].data << '\t';
while (p)
{
cout << p->adjvex << ' ' << p->weight << "\t";
p = p->next;
}
cout << endl;
}
}
数据结构---图 邻接表表示
最新推荐文章于 2022-08-24 09:01:51 发布