数据结构 图 的邻接表建立

图 的邻接表建立

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

输入信息即显示如下

相关推荐
吃着火锅x唱着歌8 小时前
LeetCode 2909.元素和最小的山形三元组II
数据结构·算法·leetcode
满天星83035778 小时前
【Linux】信号(上)
linux·运维·服务器·开发语言·c++
李日灐9 小时前
C++STL: list(双链表) 简单介绍,了解迭代器类型,list sort 的弊端
开发语言·c++·list
脏脏a9 小时前
栈 & 队列:面试题(括号 / 循环队列)+ 概念题,全考点覆盖
数据结构·栈和队列面试题
打不了嗝 ᥬ᭄9 小时前
【Linux】多路转接 Select , Poll和Epoll
linux·网络·c++·网络协议·http
啊森要自信9 小时前
【C++的奇迹之旅】map与set应用
c语言·开发语言·c++
pu_taoc9 小时前
ffmpeg实战4-将PCM与YUV封装成MP4
c++·ffmpeg·pcm
EXtreme359 小时前
算法深潜:链表中的生死之环(LeetCode 141 & 142 详解)
数据结构·算法·leetcode·链表·快慢指针·数学证明·带环链表
seven97_top9 小时前
数据结构——树
java·数据结构
2301_803554529 小时前
Pimpl(Pointer to Implementation)设计模式详解
c++·算法·设计模式