数据结构 图 的邻接表建立

图 的邻接表建立

如下图
节点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;
}

输入信息即显示如下

相关推荐
CodeSheep程序羊4 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰4 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495644 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
编程小白20264 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
数智工坊4 小时前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构
.小墨迹5 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
数智工坊5 小时前
【数据结构-树与二叉树】4.3 二叉树的存储结构
数据结构
独好紫罗兰5 小时前
对python的再认识-基于数据结构进行-a004-列表-实用事务
开发语言·数据结构·python
铉铉这波能秀5 小时前
LeetCode Hot100数据结构背景知识之列表(List)Python2026新版
数据结构·leetcode·list
历程里程碑6 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法