#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
#define MAX_VERTEX_NUM 20
const int NumEdges = 50; //边条数
const int NumVertices = 10; //顶点个数
typedef char VertexData; //顶点数据类型
typedef int EdgeData; //边上权值类型
typedef struct node { //边结点
int dest; //目标顶点下标
EdgeData cost; //边上的权值
struct node * link; //下一边链接指针
} EdgeNode;
typedef struct { //顶点结点
VertexData data; //顶点数据域
EdgeNode * firstAdj; //边链表头指针
} VertexNode;
typedef struct { //图的邻接表
VertexNode VexList [NumVertices]; //邻接表
int n, e; //图中当前的顶点个数与边数
} AdjGraph;
void CreateGraph (AdjGraph G) {
int tail,head,weight;
cout<<"请输入顶点个数和边数"<<endl;
scanf ("%d %d", &G.n, &G.e);
//输入顶点个数和边数
cout<<"请输入顶点信息"<<endl;
for ( int i = 0; i < G.n; i++) {
scanf("%c",&G.VexList[i].data ); //输入顶点信息
G.VexList[i].firstAdj = NULL;
}
cout<<"请输入边"<<endl;
for ( int i = 0; i < G.e; i++) { //逐条边输入
scanf ("%d %d %d",&tail,&head,&weight );
EdgeNode * p ;
p->dest = head;
p->cost = weight;
//链入第 tail 号链表的前端
p->link = G.VexList[tail].firstAdj;
G.VexList[tail].firstAdj = p;
p = new EdgeNode;
p->dest = tail;
p->cost = weight;
//链入第 head 号链表的前端
p->link = G.VexList[head].firstAdj;
G.VexList[head].firstAdj = p;
}
}
int main() {
AdjGraph G;
CreateGraph ( G );
return 0;
}
图的存储结构——邻接链表
最新推荐文章于 2025-05-26 19:21:32 发布