图邻接表创建无向图存储遍历

//1图的邻接矩阵表示法和邻接表创建无向图存储

#include<iostream>
using namespace std;
#define MaxNum 100		//最大顶点数

//1.1图的邻接矩阵表示法创建无向图存储
#define MaxInt  32767	//无穷
typedef char VerType;//数据类型
typedef int ArcType;//权值类型
//图结构类型
typedef struct {
	VerType  v[MaxNum];//顶点表
	ArcType a[MaxNum][MaxNum];//邻接矩阵
	int vernum, arcnum;	//点数,边数
}AMGraph;
//定位
int locate(AMGraph G, VerType v1) {
	for (int i = 0; i < G.vernum; i++) {
		if (G.v[i] == v1)
			return i;
	}
	return -1;
}
//创建无向图
bool createUDN(AMGraph &G) {
	cout << "Please input the vernum and arcnum" << endl;
	cin >> G.vernum >> G.arcnum;
	int i,j,p,q;
	VerType v1, v2;
	ArcType w;
	//初始化点的名字
	cout << "Please input the name of vernum in order " << endl;
	for (i = 0; i < G.vernum; i++) cin >> G.v[i];
	//初始化邻接矩阵
	for (i = 0; i < G.vernum; i++) {
		for (j = 0; j < G.vernum; j++) {
			if (i != j)
				G.a[i][j] = MaxInt;
			else
				G.a[i][j] = 0;
		}
	}
	//开始操作,构造邻接表
	cout << "Please input arcnum graph " << endl;
	for (i = 0; i < G.arcnum; i++) {
		cin >> v1 >> v2 >> w;
		p = locate(G, v1); q = locate(G, v2);
		G.a[p][q] = w;
		 G.a[q][p] = G.a[p][q];
	}
	return true;
}
void show(AMGraph G) {
	int i, j;
	for (i = 0; i < G.vernum; i++) {
		for (j = 0; j < G.vernum; j++) {
			if(G.a[i][j]==MaxInt)  printf("INF\t");
			else printf("%-3d\t", G.a[i][j]);
		}
		cout << endl;
	}
}
bool visited[MaxNum] = { false };
//1.深度优先搜索
void Dfs_AM(AMGraph G, int v) {//从第v个
	cout << G.v[v] << " ";
	visited[v] = true;
	for (int w = 0; w < G.vernum; w++) {
		if ((G.a[v][w] != 0 )&& (G.a[v][w] != MaxInt) && (!visited[w])) {//表示是为未被访问过的邻接点
			Dfs_AM(G, w);
		}
	}
}
//队的结构
typedef  int QElemType;
typedef struct QNode {
	QElemType node;
	struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
	QueuePtr front, rear;
}LinkQueue;
//1.初始化有带头结点的队
bool initQueue(LinkQueue &L) {
	L.front = L.rear = new QNode;
	L.front->next = NULL;
	return true;
}
//2.判空
bool isEmpty(LinkQueue L) {
	if (L.front == L.rear) return true;
	else return false;
}
//3.入队
bool EnQueue(LinkQueue &L, QElemType e) {
	QNode * p = new QNode;
	p->node = e;
	p->next = NULL;
	L.rear->next = p;
	L.rear = p;
	return true;
}
//4.出队
bool DeQueue(LinkQueue&L, QElemType &node) {
	if (isEmpty(L))
		return false;
	QNode * temp = L.front->next;//从首元结点开始出去
	node = temp->node;
	L.front->next = temp->next;
	//队最后一出队
	if (L.rear == temp) {
		L.rear = L.front;
	}
	delete temp;
	temp = NULL;
	return true;
}
//广度搜索
int FirstAdjVex(AMGraph G, int u) {
	for (int i = 0; i < G.vernum; i++) {
		if (G.a[u][i] != 0 && G.a[u][i] != MaxInt) {
			return i;
		}
	}
	return -1;
}
int NextAdjVex(AMGraph G, int u, int w) {
	for (int i = w+1; i < G.vernum; i++) {
		if (G.a[u][i] != 0 && G.a[u][i] != MaxInt) {
			return i;
		}
	}
	return -1;
}
void BFS(AMGraph G, int v) {
	LinkQueue L;
	initQueue(L);
	EnQueue(L, v);
	visited[v] = true;
	cout << G.v[v] << " ";
	while (!isEmpty(L)) {
		DeQueue(L,v);
		for (int w = FirstAdjVex(G, v); w >= 0; w = NextAdjVex(G, v, w)) {
			if (!visited[w]) {
				cout << G.v[w]<<" ";
				EnQueue(L, w); visited[w] = true;
			}
		}
	}
}
int main() {
	AMGraph G;
	createUDN(G);
	show(G);
	char ch;
	cout << "Please input the value of initial point" << endl;
	cin >> ch;
	int v = locate(G, ch);
	cout << "深度搜索" << endl;
	Dfs_AM(G, v); cout << endl;
	memset(visited, false, sizeof(visited));
	cout << "广度搜索" << endl;
	BFS(G, v);
}

/*测试案例1:
7 9
0 1 2 3 4 5 6
0 1 28
1 2 16
2 3 12
3 6 18
6 4 24
4 5 25
3 4 22
5 0 10
1 6 14
*/
//
//
/*测试案例2:
7 9
a b c d e f g
a b 28
a f 10
b c 16
b g 14
f e 25
e g 24
e d 22
c d 12
d g 18
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值