带权重无向图——C++实现创建邻接表,DFS深度遍历,BFS广度遍历

在这里插入图片描述

邻接表如图所示

这里没有加上权重,实际数据结构是有的
在这里插入图片描述

代码

#include <iostream>
#include <string>
#include <vector>
#define  VertexType string
typedef int EdgeType;
using namespace std;

/********链表队列LinkQueue************/
typedef int QElemType;

/*LinkQueue节点数据结构*/
typedef struct QNode
{
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;

/*LinkQueue数据结构*/
typedef struct
{
	QueuePtr front, rear;
}LinkQueue;

/*LinkQueue初始化*/
bool InitQueue(LinkQueue &Q)
{
	QNode *temp = new QNode;
	if (!temp) return false;
	temp->data = -1;
	temp->next = NULL;
	Q.front = Q.rear = temp;
	return true;
}

/*判断LinkQueue是否为空*/
bool isEmpty(const LinkQueue Q)
{
	return Q.front->next == NULL;
}

/*入队操作*/
bool EnQueue(LinkQueue &Q, QElemType i)
{
	QNode *temp = new QNode;
	if (!temp) return false;
	temp->data = i;
	temp->next = NULL;
	Q.rear->next = temp;
	Q.rear = temp;

	return true;
}

/*出队操作*/
QElemType DeQueue(LinkQueue &Q)
{
	QNode *ptr = Q.front->next;
	if (ptr->next)
		Q.front->next = ptr->next;
	else
		Q.front->next = NULL;
	int Data = ptr->data;
	delete(ptr);
	return Data;
}
/***********邻接表数据结构*************/

/*相邻接点的数据结构*/
typedef struct EdgeNode 
{
	int adivex;         //邻接点下标
	EdgeType weight;    //顶点与邻接点之间的权重
	struct EdgeNode *next;//指向顶点的下一个邻接点
}EdgeNode;

/*顶点的数据结构*/
typedef struct VertexNode
{
	VertexType data;    //顶点存储的数据类型
	EdgeNode *firstedge;//指向第一个邻接点
}VertexNode,*AdjList;

/*图的数据结构*/
typedef struct        
{
	AdjList adjList;
	int numVertexes, numEdges;
}GraphAdiList;

/*创建图*/
void CreateALFraph(GraphAdiList &G)
{
	cout << "请输入顶点数和边数" << endl;
	cin >> G.numVertexes >> G.numEdges;
	cout << "请输入每个顶点的名称" << endl;
	G.adjList = new VertexNode[G.numVertexes];
	for (int i = 0; i < G.numVertexes; i++)
	{
		cin >> G.adjList[i].data;
		G.adjList[i].firstedge = NULL;
	}

	int i, j ,Weight;
	for (int k = 0; k < G.numEdges; k++)
	{
		cout << "请输入第"<<k+1<<"条边上的顶点序号和权重" << endl;
		cin >> i >> j>> Weight;

		/*头插法将节点接到邻接表上*/
		EdgeNode *temp1 = new EdgeNode;
		temp1->adivex = j;
		temp1->weight = Weight;
		temp1->next = G.adjList[i].firstedge;
		G.adjList[i].firstedge = temp1;

		EdgeNode *temp2 = new EdgeNode;
		temp2->adivex = i;
		temp2->weight = Weight;
		temp2->next = G.adjList[j].firstedge;
		G.adjList[j].firstedge = temp2;	
	}
}

/*输出邻接表*/
void Display(const GraphAdiList G)
{
	for (int i = 0; i < G.numVertexes; i++)
	{
		cout << G.adjList[i].data << "->{";
		EdgeNode *ptr = G.adjList[i].firstedge;
		while (ptr)
		{
			cout << " [" << ptr->adivex<<","<<ptr->weight<<"]";
			ptr = ptr->next;
		}
		cout << " }" << endl;
	}
}

void DFS(GraphAdiList G, int i,vector<bool> &visited)
{
	visited[i] = true;
	cout << G.adjList[i].data<<" ";
	EdgeNode *ptr = G.adjList[i].firstedge;
	while (ptr)
	{
		if (!visited[ptr->adivex])
			DFS(G, ptr->adivex,visited);
		ptr = ptr->next;
	}
}
 
void DFSTraverse(GraphAdiList G, vector<bool> &visited)
{
	for (int i = 0; i < G.numVertexes; i++)
		visited[i] = false;

	/*如果是连通图就不必循环*/
	for (int i = 0; i < G.numVertexes; i++)
	{
		if (!visited[i])
			DFS(G, i, visited);
	}

	/*DFS(G, i, visited);*/
}

void BFSTraverse(GraphAdiList G, vector<bool> &visited)
{
	LinkQueue Q;
	InitQueue(Q);

	for (int i = 0; i < G.numVertexes; i++)
		visited[i] = false;

	for (int i = 0; i < G.numVertexes; i++)
	{
		if (!visited[i])
		{
			visited[i] = true;
			cout << G.adjList[i].data << " ";
			EnQueue(Q, i);
			while (!isEmpty(Q))
			{
				int index = DeQueue(Q);
				EdgeNode *ptr = G.adjList[index].firstedge;
				while (ptr)
				{
					if (!visited[ptr->adivex])
					{
						visited[ptr->adivex] = true;
						cout << G.adjList[ptr->adivex].data << " ";
						EnQueue(Q, ptr->adivex);
					}
					ptr = ptr->next;
				}
			}
		}
		
	}
}

int main()
{
	GraphAdiList Graph;
	CreateALFraph(Graph);
	vector<bool> visited(Graph.numVertexes);//定义访问数组

	cout << "邻接表如下:" << endl;
	Display(Graph);

	cout << "深度遍历" << endl;
	DFSTraverse(Graph, visited);

	cout <<endl<< "广度遍历" << endl;
	BFSTraverse(Graph, visited);

	system("pause");
}

测试效果

在这里插入图片描述
开发环境:Visual Studio 2017
参考书籍:《大话数据结构》

好的,以下是C++代码实现: ```cpp #include <iostream> #include <vector> #include <queue> using namespace std; // 定义图的边结构体 struct Edge { int to; // 边的终点 int weight; // 边的权重 Edge(int t, int w): to(t), weight(w) {} }; // 定义图的邻接表结构体 struct Graph { int n; // 节点数 vector<vector<Edge>> adj; // 邻接表 Graph(int n): n(n), adj(n) {} // 添加一条边 void addEdge(int from, int to, int weight) { adj[from].push_back(Edge(to, weight)); } // 深度优先遍历 void dfs(int cur, vector<bool>& visited) { visited[cur] = true; cout << cur << " "; for (auto e : adj[cur]) { if (!visited[e.to]) { dfs(e.to, visited); } } } // 广度优先遍历 void bfs(int cur, vector<bool>& visited) { queue<int> q; q.push(cur); visited[cur] = true; while (!q.empty()) { int u = q.front(); q.pop(); cout << u << " "; for (auto e : adj[u]) { if (!visited[e.to]) { visited[e.to] = true; q.push(e.to); } } } } }; int main() { int n, m; cin >> n >> m; Graph g(n); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; g.addEdge(u, v, w); g.addEdge(v, u, w); // 无向图需要添加反向边 } // 深度优先遍历 cout << "DFS: "; vector<bool> visited(n, false); g.dfs(0, visited); cout << endl; // 广度优先遍历 cout << "BFS: "; visited.assign(n, false); g.bfs(0, visited); cout << endl; return 0; } ``` 注释: 1. 定义了边结构体 `Edge` 和邻接表结构体 `Graph`,其中 `Edge` 包含了边的终点和权重,`Graph` 包含了节点数和邻接表数组。 2. `addEdge` 方法用于向邻接表中添加一条边,无向图需要添加反向边。 3. `dfs` 方法用于执行深度优先遍历,使用递归实现。`visited` 数组用于记录节点是否被访问过。 4. `bfs` 方法用于执行广度优先遍历,使用队列实现。`visited` 数组用于记录节点是否被访问过。 5. 在 `main` 函数中,首先读入节点数和边数,然后通过一个循环读入每条边的起点、终点和权重,最后执行深度优先遍历广度优先遍历
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值