当前位置:首页 > 通信资讯 > 正文

图的邻接表c语言(c++邻接表构建无向图)

本文实例为大家分享了C++有向图的邻接表表示,供大家参考,具体内容如下

一、思路:

有向图的插入有向边、删除边、删除顶点和无向图的有区别。其他的和无向图的类似。

1.插入有向边<e1, e2>

只需要插入<e1, e2>边就行,不需要插入对称边<e2, e1>

2.删除边<e1,e2>:

只需要删除<e1, e2>边就行,不需要仔找对称边<e2, e1>进行删除。

3.删除顶点v:

首先,要在邻接表中删除以v为头的边<v, w>;

同时,也要在邻接表中删除以v为尾的边<k, v>, 不能通过对称边来找,只能一个个顶点找,浪费时间。

二、实现程序

1.DirectedGraph.h:有向图

  1. #ifndef DirectedGraph_h
  2. #define DirectedGraph_h
  3. #include <iostream>
  4. using namespace std;
  5. const int DefaultVertices = 30;
  6. template <class T, class E>
  7. struct Edge { // 边结点的定义
  8. int dest; // 边的另一顶点位置
  9. E cost; // 表上的权值
  10. Edge<T, E> *link; // 下一条边链指针
  11. };
  12. template <class T, class E>
  13. struct Vertex { // 顶点的定义
  14. T data; // 顶点的名字
  15. Edge<T, E> *adj; // 边链表的头指针
  16. };
  17. template <class T, class E>
  18. class Graphlnk {
  19. public:
  20. const E maxValue = 100000; // 代表无穷大的值(=∞)
  21. Graphlnk(int sz=DefaultVertices); // 构造函数
  22. ~Graphlnk(); // 析构函数
  23. void inputGraph(); // 建立邻接表表示的图
  24. void outputGraph(); // 输出图中的所有顶点和边信息
  25. T getValue(int i); // 取位置为i的顶点中的值
  26. E getWeight(int v1, int v2); // 返回边(v1, v2)上的权值
  27. bool insertVertex(const T& vertex); // 插入顶点
  28. bool insertEdge(int v1, int v2, E weight); // 插入边
  29. bool removeVertex(int v); // 删除顶点
  30. bool removeEdge(int v1, int v2); // 删除边
  31. int getFirstNeighbor(int v); // 取顶点v的第一个邻接顶点
  32. int getNextNeighbor(int v,int w); // 取顶点v的邻接顶点w的下一邻接顶点
  33. int getVertexPos(const T vertex); // 给出顶点vertex在图中的位置
  34. int numberOfVertices(); // 当前顶点数
  35. private:
  36. int maxVertices; // 图中最大的顶点数
  37. int numEdges; // 当前边数
  38. int numVertices; // 当前顶点数
  39. Vertex<T, E> * nodeTable; // 顶点表(各边链表的头结点)
  40. };
  41. // 构造函数:建立一个空的邻接表
  42. template <class T, class E>
  43. Graphlnk<T, E>::Graphlnk(int sz) {
  44. maxVertices = sz;
  45. numVertices = 0;
  46. numEdges = 0;
  47. nodeTable = new Vertex<T, E>[maxVertices]; // 创建顶点表数组
  48. if(nodeTable == NULL) {
  49. cerr << "存储空间分配错误!" << endl;
  50. exit(1);
  51. }
  52. for(int i = 0; i < maxVertices; i++)
  53. nodeTable[i].adj = NULL;
  54. }
  55. // 析构函数
  56. template <class T, class E>
  57. Graphlnk<T, E>::~Graphlnk() {
  58. // 删除各边链表中的结点
  59. for(int i = 0; i < numVertices; i++) {
  60. Edge<T, E> *p = nodeTable[i].adj; // 找到其对应链表的首结点
  61. while(p != NULL) { // 不断地删除第一个结点
  62. nodeTable[i].adj = p->link;
  63. delete p;
  64. p = nodeTable[i].adj;
  65. }
  66. }
  67. delete []nodeTable; // 删除顶点表数组
  68. }
  69. // 建立邻接表表示的图
  70. template <class T, class E>
  71. void Graphlnk<T, E>::inputGraph() {
  72. int n, m; // 存储顶点树和边数
  73. int i, j, k;
  74. T e1, e2; // 顶点
  75. E weight; // 边的权值
  76. cout << "请输入顶点数和边数:" << endl;
  77. cin >> n >> m;
  78. cout << "请输入各顶点:" << endl;
  79. for(i = 0; i < n; i++) {
  80. cin >> e1;
  81. insertVertex(e1); // 插入顶点
  82. }
  83. cout << "请输入图的各边的信息:" << endl;
  84. i = 0;
  85. while(i < m) {
  86. cin >> e1 >> e2 >> weight;
  87. j = getVertexPos(e1);
  88. k = getVertexPos(e2);
  89. if(j == -1 || k == -1)
  90. cout << "边两端点信息有误,请重新输入!" << endl;
  91. else {
  92. insertEdge(j, k, weight); // 插入边
  93. i++;
  94. }
  95. } // while
  96. }
  97. // 输出有向图中的所有顶点和边信息
  98. template <class T, class E>
  99. void Graphlnk<T, E>::outputGraph() {
  100. int n, m, i;
  101. T e1, e2; // 顶点
  102. E weight; // 权值
  103. Edge<T, E> *p;
  104. n = numVertices;
  105. m = numEdges;
  106. cout << "图中的顶点数为" << n << ",边数为" << m << endl;
  107. for(i = 0; i < n; i++) {
  108. p = nodeTable[i].adj;
  109. while(p != NULL) {
  110. e1 = getValue(i); // 有向边<i, p->dest>
  111. e2 = getValue(p->dest);
  112. weight = p->cost;
  113. cout << "<" << e1 << ", " << e2 << ", " << weight << ">" << endl;
  114. p = p->link; // 指向下一个邻接顶点
  115. }
  116. }
  117. }
  118. // 取位置为i的顶点中的值
  119. template <class T, class E>
  120. T Graphlnk<T, E>::getValue(int i) {
  121. if(i >= 0 && i < numVertices)
  122. return nodeTable[i].data;
  123. return NULL;
  124. }
  125. // 返回边(v1, v2)上的权值
  126. template <class T, class E>
  127. E Graphlnk<T, E>::getWeight(int v1, int v2) {
  128. if(v1 != -1 && v2 != -1) {
  129. Edge<T , E> *p = nodeTable[v1].adj; // v1的第一条关联的边
  130. while(p != NULL && p->dest != v2) { // 寻找邻接顶点v2
  131. p = p->link;
  132. }
  133. if(p != NULL)
  134. return p->cost;
  135. }
  136. return maxValue; // 边(v1, v2)不存在,就存放无穷大的值
  137. }
  138. // 插入顶点
  139. template <class T, class E>
  140. bool Graphlnk<T, E>::insertVertex(const T& vertex) {
  141. if(numVertices == maxVertices) // 顶点表满,不能插入
  142. return false;
  143. nodeTable[numVertices].data = vertex; // 插入在表的最后
  144. numVertices++;
  145. return true;
  146. }
  147. // 插入边
  148. template <class T, class E>
  149. bool Graphlnk<T, E>::insertEdge(int v1, int v2, E weight) {
  150. if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) {
  151. Edge<T, E> *p = nodeTable[v1].adj; // v1对应的边链表头指针
  152. while(p != NULL && p->dest != v2) // 寻找邻接顶点v2
  153. p = p->link;
  154. if(p != NULL) // 已存在该边,不插入
  155. return false;
  156. p = new Edge<T, E>; // 创建新结点
  157. p->dest = v2;
  158. p->cost = weight;
  159. p->link = nodeTable[v1].adj; // 链入v1边链表
  160. nodeTable[v1].adj = p;
  161. numEdges++;
  162. return true;
  163. }
  164. return false;
  165. }
  166. // 有向图删除顶点较麻烦
  167. template <class T, class E>
  168. bool Graphlnk<T, E>::removeVertex(int v) {
  169. if(numVertices == 1 || v < 0 || v > numVertices)
  170. return false; // 表空或顶点号超出范围
  171. Edge<T, E> *p, *s;
  172. // 1.清除顶点v的边链表结点w 边<v,w>
  173. while(nodeTable[v].adj != NULL) {
  174. p = nodeTable[v].adj;
  175. nodeTable[v].adj = p->link;
  176. delete p;
  177. numEdges--; // 与顶点v相关联的边数减1
  178. } // while结束
  179. // 2.清除<w, v>,与v有关的边
  180. for(int i = 0; i < numVertices; i++) {
  181. if(i != v) { // 不是当前顶点v
  182. s = NULL;
  183. p = nodeTable[i].adj;
  184. while(p != NULL && p->dest != v) {// 在顶点i的链表中找v的顶点
  185. s = p;
  186. p = p->link; // 往后找
  187. }
  188. if(p != NULL) { // 找到了v的结点
  189. if(s == NULL) { // 说明p是nodeTable[i].adj
  190. nodeTable[i].adj = p->link;
  191. } else {
  192. s->link = p->link; // 保存p的下一个顶点信息
  193. }
  194. delete p; // 删除结点p
  195. numEdges--; // 与顶点v相关联的边数减1
  196. }
  197. }
  198. }
  199. numVertices--; // 图的顶点个数减1
  200. nodeTable[v].data = nodeTable[numVertices].data; // 填补,此时numVertices,比原来numVertices小1,所以,这里不需要numVertices-1
  201. nodeTable[v].adj = nodeTable[numVertices].adj;
  202. // 3.要将填补的顶点对应的位置改写
  203. for(int i = 0; i < numVertices; i++) {
  204. p = nodeTable[i].adj;
  205. while(p != NULL && p->dest != numVertices) // 在顶点i的链表中找numVertices的顶点
  206. p = p->link; // 往后找
  207. if(p != NULL) // 找到了numVertices的结点
  208. p->dest = v; // 将邻接顶点numVertices改成v
  209. }
  210. return true;
  211. }
  212. // 删除边
  213. template <class T, class E>
  214. bool Graphlnk<T, E>::removeEdge(int v1, int v2) {
  215. if(v1 != -1 && v2 != -1) {
  216. Edge<T, E> * p = nodeTable[v1].adj, *q = NULL;
  217. while(p != NULL && p->dest != v2) { // v1对应边链表中找被删除边
  218. q = p;
  219. p = p->link;
  220. }
  221. if(p != NULL) { // 找到被删除边结点
  222. if(q == NULL) // 删除的结点是边链表的首结点
  223. nodeTable[v1].adj = p->link;
  224. else
  225. q->link = p->link; // 不是,重新链接
  226. delete p;
  227. return true;
  228. }
  229. }
  230. return false; // 没有找到结点
  231. }
  232. // 取顶点v的第一个邻接顶点
  233. template <class T, class E>
  234. int Graphlnk<T, E>::getFirstNeighbor(int v) {
  235. if(v != -1) {
  236. Edge<T, E> *p = nodeTable[v].adj; // 对应链表第一个边结点
  237. if(p != NULL) // 存在,返回第一个邻接顶点
  238. return p->dest;
  239. }
  240. return -1; // 第一个邻接顶点不存在
  241. }
  242. // 取顶点v的邻接顶点w的下一邻接顶点
  243. template <class T, class E>
  244. int Graphlnk<T, E>::getNextNeighbor(int v,int w) {
  245. if(v != -1) {
  246. Edge<T, E> *p = nodeTable[v].adj; // 对应链表第一个边结点
  247. while(p != NULL && p->dest != w) // 寻找邻接顶点w
  248. p = p->link;
  249. if(p != NULL && p->link != NULL)
  250. return p->link->dest; // 返回下一个邻接顶点
  251. }
  252. return -1; // 下一个邻接顶点不存在
  253. }
  254. // 给出顶点vertex在图中的位置
  255. template <class T, class E>
  256. int Graphlnk<T, E>::getVertexPos(const T vertex) {
  257. for(int i = 0; i < numVertices; i++)
  258. if(nodeTable[i].data == vertex)
  259. return i;
  260. return -1;
  261. }
  262. // 当前顶点数
  263. template <class T, class E>
  264. int Graphlnk<T, E>::numberOfVertices() {
  265. return numVertices;
  266. }
  267. #endif /* DirectedGraph_h */

2.main.cpp

  1. /*
  2. 测试数据:
  3. 5 7
  4. 0 1 2 3 4
  5. 0 1 10
  6. 0 3 30
  7. 0 4 100
  8. 1 2 50
  9. 2 4 10
  10. 3 2 20
  11. 3 4 60
  12. */
  13. #include "DirectedGraph.h"
  14. int main(int argc, const char * argv[]) {
  15. Graphlnk<char, int> st; // 声明对象
  16. bool finished = false;
  17. int choice;
  18. char e1, e2, next;
  19. int weight;
  20. while(!finished) {
  21. cout << "[1]创建基于邻接表的有向图" << endl;
  22. cout << "[2]输出图的所有顶点和边信息" << endl;
  23. cout << "[3]取顶点v的第一个邻接顶点" << endl;
  24. cout << "[4]取v的邻接顶点w的下一个邻接顶点" << endl;
  25. cout << "[5]插入顶点" << endl;
  26. cout << "[6]插入边" << endl;
  27. cout << "[7]删除顶点" << endl;
  28. cout << "[8]删除边" << endl;
  29. cout << "[9]退出" << endl;
  30. cout << "请输入选择[1-9]:";
  31. cin >> choice;
  32. switch(choice) {
  33. case 1:
  34. st.inputGraph();
  35. break;
  36. case 2:
  37. st.outputGraph();
  38. break;
  39. case 3:
  40. cout << "请输入顶点:";
  41. cin >> e1;
  42. e2 = st.getValue(st.getFirstNeighbor(st.getVertexPos(e1)));
  43. if(e2)
  44. cout << "顶点" << e1 << "的第一个邻接顶点为:" << e2 << endl;
  45. else
  46. cout << "顶点" << e1 << "没有邻接顶点!" << endl;
  47. break;
  48. case 4:
  49. cout << "请输入顶点v和邻接顶点w:";
  50. cin >> e1 >> e2;
  51. next = st.getValue(st.getNextNeighbor(st.getVertexPos(e1), st.getVertexPos(e2)));
  52. if(next)
  53. cout << "顶点" << e1 << "的邻接顶点" << e2 << "的下一个邻接顶点为:" << next << endl;
  54. else
  55. cout << "顶点" << e1 << "的邻接顶点" << e2 << "没有下一个邻接顶点!" << endl;
  56. break;
  57. case 5:
  58. cout << "请输入要插入的顶点:";
  59. cin >> e1;
  60. if(st.insertVertex(e1))
  61. cout << "插入成功!" << endl;
  62. else
  63. cout << "表已满,插入失败!" << endl;
  64. break;
  65. case 6:
  66. cout << "请输入要插入的边的信息:" << endl;
  67. cin >> e1 >> e2 >> weight;
  68. st.insertEdge(st.getVertexPos(e1), st.getVertexPos(e2), weight);
  69. break;
  70. case 7:
  71. cout << "请输入要删除的顶点:";
  72. cin >> e1;
  73. if(st.removeVertex(st.getVertexPos(e1)))
  74. cout << "顶点" << e1 << "已删除!" << endl;
  75. else
  76. cout << "顶点" << e1 << "不在图中!" << endl;
  77. break;
  78. case 8:
  79. cout << "请输入要删除的边的两个端点:" << endl;
  80. cin >> e1 >> e2;
  81. st.removeEdge(st.getVertexPos(e1), st.getVertexPos(e2));
  82. break;
  83. case 9:
  84. finished = true;
  85. break;
  86. default:
  87. cout << "选择输入错误,请重新输入!" << endl;
  88. }
  89. }
  90. return 0;
  91. }

测试结果:

图的邻接表c语言(c++邻接表构建无向图)

图的邻接表c语言(c++邻接表构建无向图)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/chuanzhouxiao/article/details/88828422

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。