数据结构---图(邻接列表)


> 图


: 在计算机科学中,一个图就是一些顶点的集合,这些顶点通过一系列边结对(连接)。顶点用圆圈表示,边就
是这些圆圈之间的连线。顶点之间通过边连接。
在这里插入图片描述

结构体:
首先一个顶点有两个数据,一个是数据,一个是边。
然后边里包括定点连接的哪条边,权重,顶点的下一条边。
之后就是定义图了,图里包括指向所有顶点的指针,顶点的个数以及边。
visted用来保存该节点是否被访问过。

bool visted[1024];
typedef struct _EdgeNode {
	int adj;
	int weight;
	struct _EdgeNode* next;
}EdgeNode;
typedef struct _VertexNode {
	char c;
	struct _EdgeNode* frist;
}VertexNode;
typedef struct _AdjListGraph {
	VertexNode* adjlist;
	int ver;
	int edg;
}AdjListGraph;

初始化:
先将给指针分配内存空间
再将边和节点个数设置为0

void init(AdjListGraph& G) {
	G.adjlist = new VertexNode[1024];
	G.edg = 0;
	G.ver = 0;
	for (int i = 0;i < 1024;i++) {
		visted[i] = false;
	}
}

查询:
用过数据查询是否有该顶点,如果有的话,返回它的下标
这里的意思就是,数据和下标之间的转换。

int location(AdjListGraph& G, int e) {
	for (int i = 0;i < G.ver;i++) {
		if (G.adjlist[i].c == e)
			return i;
	}
	return -1;
}

建立:
首先输入节点个数和边数
再分别输顶点的数据
再输入边之间的关系(建立两个边之间的关系之前通过查询函数,判断是否有这两个顶点。)建立的方法为,设置一个新的边,adj的值赋为第二个顶点的值,将它放置到第一个顶点的放置边的指针中。

void Create(AdjListGraph& G) {
	cout << "请输入图的节点个数和边数:" << endl;
	cin >> G.ver >> G.edg;
	cout << "请输入节点的数据:" << endl;
	for (int i = 0;i < G.ver;i++) {
		cin >> G.adjlist[i].c;
		G.adjlist[i].frist = NULL;
	}
	char c1, c2;
	int i1, i2;
	cout << "请输入边的关系:" << endl;
	for (int i = 0;i < G.edg;i++) {
		cin >> c1 >> c2;
		i1 = location(G, c1);
		i2 = location(G, c2);
		if (i1 != -1 && i2 != -1) {
			EdgeNode* E = new EdgeNode;
			E->adj = i2;
			E->next = G.adjlist[i1].frist;
			G.adjlist[i1].frist = E;
		}
	}
}

广度遍历:
BFS_main:遍历所有的节点
BFS:类似于树的遍历,利用了队列,先判断顶点,是否已经被遍历过了,如果没有,输出,然后将它的边都放到队列中,这样做保证了,先遍立第一层顶点,在遍历其他层

void BFS_main(AdjListGraph& G) {
	for (int i = 0;i < G.ver;i++) {
		BFS(G,i);
	}
}
void BFS(AdjListGraph& G, int v) {
	queue <int> q;
	int cur = -1;
	int index = -1;
	q.push(v);
	while (!q.empty()) {
		cur = q.front();
		if (visted[cur] == false) {
			cout << G.adjlist[cur].c << " ";
			visted[cur] = true;
		}
		q.pop();
		EdgeNode* tmp = G.adjlist[cur].frist;
		while (tmp) {
			index = tmp->adj;
			tmp = tmp->next;
			q.push(index);
		}
	}
}

全部代码:

#include<iostream>
#include<Windows.h>
#include<queue>
using namespace std;
bool visted[1024];
typedef struct _EdgeNode {
	int adj;
	int weight;
	struct _EdgeNode* next;
}EdgeNode;
typedef struct _VertexNode {
	char c;
	struct _EdgeNode* frist;
}VertexNode;
typedef struct _AdjListGraph {
	VertexNode* adjlist;
	int ver;
	int edg;
}AdjListGraph;
void init(AdjListGraph& G);
void Create(AdjListGraph& G);
int location(AdjListGraph& G, int e);
void BFS(AdjListGraph& G, int v);
void BFS_main(AdjListGraph& G);
int main() {
	AdjListGraph G;
	init(G);
	Create(G);
	BFS_main(G);
	system("pause");
	return 0;
}
void BFS(AdjListGraph& G, int v) {
	queue <int> q;
	int cur = -1;
	int index = -1;
	q.push(v);
	while (!q.empty()) {
		cur = q.front();
		if (visted[cur] == false) {
			cout << G.adjlist[cur].c << " ";
			visted[cur] = true;
		}
		q.pop();
		EdgeNode* tmp = G.adjlist[cur].frist;
		while (tmp) {
			index = tmp->adj;
			tmp = tmp->next;
			q.push(index);
		}
	}
}
void BFS_main(AdjListGraph& G) {
	for (int i = 0;i < G.ver;i++) {
		BFS(G,i);
	}
}
void init(AdjListGraph& G) {
	G.adjlist = new VertexNode[1024];
	G.edg = 0;
	G.ver = 0;
	for (int i = 0;i < 1024;i++) {
		visted[i] = false;
	}
}
void Create(AdjListGraph& G) {
	cout << "请输入图的节点个数和边数:" << endl;
	cin >> G.ver >> G.edg;
	cout << "请输入节点的数据:" << endl;
	for (int i = 0;i < G.ver;i++) {
		cin >> G.adjlist[i].c;
		G.adjlist[i].frist = NULL;
	}
	char c1, c2;
	int i1, i2;
	cout << "请输入边的关系:" << endl;
	for (int i = 0;i < G.edg;i++) {
		cin >> c1 >> c2;
		i1 = location(G, c1);
		i2 = location(G, c2);
		if (i1 != -1 && i2 != -1) {
			EdgeNode* E = new EdgeNode;
			E->adj = i2;
			E->next = G.adjlist[i1].frist;
			G.adjlist[i1].frist = E;
		}
	}
}
int location(AdjListGraph& G, int e) {
	for (int i = 0;i < G.ver;i++) {
		if (G.adjlist[i].c == e)
			return i;
	}
	return -1;
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值