带权重无向图——C++实现创建邻接表,DFS深度遍历,BFS广度遍历
2020-03-02 03:28
633 查看
邻接表如图所示
这里没有加上权重,实际数据结构是有的
代码
#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++ 无向图 邻接表创建 广度遍历 深度遍历
- 邻接表深度和广度遍历DFS_BFS.c
- 深度遍历(DFS)与广度遍历(BFS) (C语言实现)
- 数据结构-图-邻接表深度和广度遍历DFS_BFS
- 邻接表深度优先和广度优先遍历(DFS和BFS)
- BFS_DFS深度广度优先C++实现。
- 大话数据结构 code 第七章 04邻接表深度和广度遍历DFS_BFS
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
- [置顶] 图:图的邻接表创建、深度优先遍历和广度优先遍历代码实现
- 邻接表实现--图的深度优先遍历DFS和广度优先遍历BFS
- 【数据结构】邻接表深度和广度遍历DFS_BFS
- 图的广度遍历(BFS)与深度遍历(DFS)
- 数据结构之——用C++实现邻接表的DFS与BFS
- 图的遍历 DFS(深度优先),BFS(广度优先)
- 【数据结构】拾遗(一):图的邻接矩阵创建以及其深广度遍历C++实现
- 邻接矩阵实现--图的深度优先遍历DFS和广度优先遍历BFS
- 图的存储及遍历 深度遍历和广度遍历 C++代码实现
- 基于邻接表的无向图的深度广度遍历实现
- python遍历文件夹——深度优先(DFS)/广度优先(BFS)
- 图的遍历之-DFS深度优先遍历C++实现