带权重无向图——创建邻接表,DFS深度遍历

在这里插入图片描述

邻接表如图所示

在这里插入图片描述

代码

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

/*相邻接点的数据结构*/
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);*/
}

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

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

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

	system("pause");
}

测试效果

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

发布了10 篇原创文章 · 获赞 2 · 访问量 832

猜你喜欢

转载自blog.csdn.net/qq_41195985/article/details/104298975