#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXVEX 100
//邻接表
//存储结构
typedef char VertexType;//定义顶点的类型
typedef int EdgeType;//定义边的类型
typedef struct EdgeNode //定义边对应顶点的结构
{
int adjvex;//定义邻接点域
EdegeType weight;//网络中边所对应的权值
struct EdgeType *next;
}EdgeNode;
//定义顶点表的结点
typedef struct VertexNode
{
VertexType data;
EdgeNode *firstedge;
}VertexNode,AdjList[MAXVEX];
//图的结构
typedef struct
{
AdjList adjList; //顶点表的节点数组
int numVertexes,numEdges; //定义顶点数,边数
}GraphAdjList;
//创建图的邻接表的结构
void CreateALGraph(GraphAdjList *G)
{
int i,j,k;
EdgeNode *e;
cout<<"输入定点数和边数"<<endl;
cin>>G->numVertexes>>G->numEdges;
cout<<"输入每个顶点的值"<<endl;
for(i=0;i<G->numVertexes;i++)
{
cin>>G->adjList[i]->data;
G->adjList[i]->firstedge=NULL;
}
for(k=0;k<G->numEdges;k++)
{
cout<<"输入边vi vj的顶点编号:"<<endl;
cin>>i>>j;
//下面的类似于线性表的链表的头插法
//首先时当vi作为顶点表
e=(EdegNode *)malloc(sizeof(EdgeNode));
e->adjvex=j;
e->next=G->adjList[i]->firstedge;
G->adjList[i]->firstedge=e;
//当vj作为顶点表
e=(EdegNode *)malloc(sizeof(EdgeNode));
e->adjvex=i;
e->next=G->adjList[j]->firstedge;
G->adjList[j]->firstedge=e;
}
}
数据结构那点事--图(邻接表)
最新推荐文章于 2023-04-07 20:40:57 发布