数据结构 图 的邻接表建立

图 的邻接表建立

如下图
节点A没有指向任何节点
节点B指向A,也指向C,还指向E.
节点C指向E.
节点D指向C.
节点E指向B,也指向C,还指向D.
将其抽象建立为右下的邻接表

cpp 复制代码
typedef struct _EdgeNode {//与节点连接的边的定义
	int adjvedx; //指向终点顶点的下标
	int weight; //权重
	_EdgeNode* next; //下一条边
}EdgeNode;

typedef struct _VertexNode { //顶点节点
	char data; //节点数据
	_EdgeNode* first; //指向邻接第一条边
}VertexNode,AdjList;

typedef struct _AdjListGraph {
	AdjList* adjlist;
	int vex; //顶点数
	int edge; //边数
}AdjListGraph;

在内存中的建立结构如下:

参考程序如下:

Graphs.h

cpp 复制代码
#pragma once
#include <iostream>

using namespace std;
   
#define  MAXSIZE   10 

typedef struct _EdgeNode {//与节点连接的边的定义
	int adjvedx; //邻接的顶点
	int weight; //权重
	_EdgeNode* next; //下一条边
}EdgeNode;

typedef struct _VertexNode { //顶点节点
	char data; //节点数据
	_EdgeNode* first; //指向邻接第一条边
}VertexNode,AdjList;

typedef struct _AdjListGraph {
	AdjList* adjlist;
	int vex; //顶点数
	int edge; //边数
}AdjListGraph;

//图的初始化
void Init(AdjListGraph& G);

//图的创建(邻接表的创建)
void Create(AdjListGraph& G);

//通过顶点对应的字符寻找顶点在顶点列表中的位置
int Location(AdjListGraph& G, char c);

//输出构建的邻接表
void printGraphs(AdjListGraph& G);

//销毁邻接表
void Destroy(AdjListGraph& G);

Graphs.cpp

cpp 复制代码
#include "Graphs.h"

void Init(AdjListGraph& G)
{
	G.adjlist = new AdjList[MAXSIZE];
	G.edge = 0;
	G.vex = 0;
}

void Create(AdjListGraph& G)
{
	cout << "请输入该图的顶点数:" << endl;
	cin >> G.vex;
	cout << "请输入该图的边数:" << endl;
	cin >> G.edge;


	cout << "请输入相关顶点:" << endl;
	for (int i = 0; i < G.vex; i++) {
		cin >> G.adjlist[i].data;
		G.adjlist[i].first = NULL;
	}

	char v1 = 0, v2 = 0; //保存输入的顶点的字符
	int i1, i2; //保存顶点在数组中的下标

	cout << "请输入想关联边的顶点(A-->B):" << endl;
	for (int i = 0; i < G.edge; i++) {
		cout << "起点-->终点:";
		cin >> v1 >> v2;

		i1 = Location(G, v1);
		i2 = Location(G, v2);

		if (i1 != -1 && i2 != -1) { //寻找到位置
			EdgeNode* temp = new EdgeNode;
			temp->adjvedx = i2; //边指向的顶点位置
			temp->next = G.adjlist[i1].first; //置空
			G.adjlist[i1].first = temp; //链接到顶点表上
		}
	}
}

int Location(AdjListGraph& G, char c)
{
	for (int i = 0; i < G.vex; i++) {
		if (G.adjlist[i].data == c) {
			return i;
		}
	}
	return -1;
}

void printGraphs(AdjListGraph& G)
{
	cout << "邻接表顶点数:" << G.vex << 
		"  邻接表边数:" << G.edge << endl;

	for (int i = 0; i < G.vex; i++) {
		cout << "顶点:" << G.adjlist[i].data;
		EdgeNode* p = G.adjlist[i].first;
		while (p) {
			int n = p->adjvedx; //边指向顶点的位置
			cout << "-->" << G.adjlist[n].data;
			p = p->next;
		}

		cout << endl;
	}

}

void Destroy(AdjListGraph& G)
{
	EdgeNode* p = NULL, * tmp = NULL;

	for (int i = 0; i < G.vex; i++) {
		p=G.adjlist[i].first;
		while (p) {
			tmp = p;
			p = p->next;
			delete tmp;
		}
	}
	delete[] G.adjlist;
}

main.cpp

cpp 复制代码
#include "Graphs.h"

int main() {
	AdjListGraph Graph;

	Init(Graph);

	Create(Graph);

	printGraphs(Graph);

	Destroy(Graph);

	system("pause");
	return 0;
}

输入信息即显示如下

相关推荐
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4563 天前
C++进阶(1)——前景提要
c++
夜悊4 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴4 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0014 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
小小工匠4 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾4 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you4 天前
constexpr函数
c++
Qres8214 天前
算法复键——树状数组
数据结构·算法
凡人叶枫4 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++