[C++] 数据结构与算法----4.6 用邻接表储存图,用广度优先搜索策略判断图的2个顶点 →→→→→进入此内容的聊天室

来自 , 2021-02-04, 写在 C++, 查看 156 次.
URL http://www.code666.cn/view/df4fe8a8
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. #include<iostream>
  5. using namespace std;
  6. const int MVN = 21;
  7. typedef char VexType;//顶点元素类型
  8. typedef int QElemType;
  9. typedef struct QNode
  10. {
  11.         QElemType data;
  12.         QNode *next;
  13. }*LQueuePtr;
  14. typedef struct ArcNode
  15. {
  16.         int adjvex;//弧指向的顶点位置
  17.         int adj;//权
  18.         ArcNode *nextarc;//指向下一条弧的顶点
  19. }*ArcPtr;
  20. struct VexNode
  21. {
  22.         VexType data;//顶点数据
  23.         ArcPtr firstarc;//指向第一条依附于该顶点的弧
  24.         bool visited;//是否被访问过
  25. };
  26. struct ALGraph
  27. {
  28.         VexNode vexs[MVN];
  29.         int kind, vexnum, arcnum;//类型,顶点数,弧数
  30. };
  31. struct LQueue
  32. {
  33.         LQueuePtr front, rear;//队头指针,队尾指针
  34. };
  35. int LocateVex(ALGraph &G, VexType v);//查找顶点
  36. bool InsertArc(ALGraph &G, VexType u, VexType v, int w = 1);//插入弧
  37. void CreateGraph(ALGraph &G, int Kind, char v[]);
  38. void GraphPrint(ALGraph &G);
  39. void QueueInit(LQueue &Q);
  40. void Enqueue(LQueue &Q, QElemType e);
  41. bool Dequeue(LQueue &Q, QElemType &e);
  42. void visit(VexType u);
  43. bool existroad(ALGraph &G, VexType u, VexType v);
  44.  
  45. int main()
  46. {
  47.         char g[] = "sacdw#sascsdacadcd#";
  48.         cout << "序列为 "<< endl<< g<< endl;
  49.         int Kind = 3;
  50.         ALGraph G;
  51.         CreateGraph(G, Kind, g);
  52.         GraphPrint(G);
  53.         VexType u = 'a';
  54.         //VexType v1 = 'c';
  55.         VexType v1 = 'w';
  56.         bool f1 =existroad(G, u, v1);
  57.         cout <<u<<"到"<<v1<<"的路径"<< (f1?"":"不")<<"存在" << endl;
  58.  
  59.         return 0;
  60. }
  61. void QueueInit(LQueue &Q)
  62. {
  63.         Q.front = new QNode;
  64.         Q.front->next = NULL;
  65.         Q.rear = Q.front;
  66. }
  67. void Enqueue(LQueue &Q, QElemType e)
  68. {
  69.         LQueuePtr p;
  70.         p = new QNode;
  71.         p->data = e;
  72.         p->next = NULL;
  73.         Q.rear->next = p;
  74.         Q.rear = p;
  75. }
  76. bool Dequeue(LQueue &Q, QElemType &e)
  77. {
  78.         LQueuePtr p;
  79.         if (Q.rear == Q.front)
  80.         {
  81.                 return false;
  82.         }
  83.         p = Q.front;
  84.         Q.front = p->next;
  85.         e = Q.front->data;
  86.         delete p;
  87.         return true;
  88. }
  89. void visit(VexType u)
  90. {
  91.         cout << u << ",";
  92. }
  93. bool existroad(ALGraph &G, VexType u, VexType v)
  94. {
  95.         int i, j, k, m;
  96.         ArcPtr p;
  97.         LQueue q;
  98.         m = LocateVex(G, u);
  99.         if (m == 0)
  100.         {
  101.                 return false;
  102.         }
  103.         if (!G.vexs[m].firstarc)
  104.         {
  105.                 return false;
  106.         }
  107.         for (i = 1; i <= G.vexnum; i++)
  108.         {
  109.                 G.vexs[i].visited = false;
  110.         }
  111.         G.vexs[m].visited = true;
  112.         QueueInit(q);
  113.         Enqueue(q, m);
  114.         while (Dequeue(q, j))
  115.         {
  116.                 for (p = G.vexs[j].firstarc; p; p = p->nextarc)
  117.                 {
  118.  
  119.                         k = p->adjvex;
  120.                         if (G.vexs[k].visited)
  121.                         {
  122.                                 continue;
  123.                         }
  124.                         if (G.vexs[k].data == v)
  125.                         {
  126.                                 return true;
  127.                         }
  128.                         G.vexs[k].visited = true;
  129.                         Enqueue(q, k);
  130.  
  131.                 }
  132.         }
  133.         return false;
  134.  
  135. }
  136. int LocateVex(ALGraph &G, VexType v)//查找顶点
  137. {
  138.         int i;
  139.         for (i = G.vexnum; i > 0 && G.vexs[i].data != v; i--);
  140.         return i;
  141. }
  142. bool InsertArc(ALGraph &G, VexType u, VexType v, int w )
  143. {
  144.         ArcPtr p;
  145.         int i, j;
  146.         bool found;
  147.         i = LocateVex(G, u);
  148.         if (i == 0)
  149.         {
  150.                 return false;
  151.         }
  152.         j = LocateVex(G, v);
  153.         if (j == 0 || j == i)
  154.         {
  155.                 return false;
  156.         }
  157.         for (found = false, p = G.vexs[i].firstarc; p && !found; p = p->nextarc)
  158.         {
  159.                 if (p->adjvex == j)
  160.                 {
  161.                         found = true;
  162.                 }
  163.         }
  164.         if (found)
  165.         {
  166.                 return false;
  167.         }
  168.         G.arcnum++;
  169.         p = new ArcNode;
  170.         p->adjvex = j;
  171.         p->adj = w;
  172.         p->nextarc = G.vexs[i].firstarc;
  173.         G.vexs[i].firstarc = p;//表头插入弧,假设对弧的次序无要求
  174.         if (G.kind <= 2)
  175.         {
  176.                 return true;
  177.         }
  178.         G.arcnum++;
  179.         p = new ArcNode;
  180.         p->adjvex = i;
  181.         p->adj = w;
  182.         p->nextarc = G.vexs[j].firstarc;
  183.         G.vexs[j].firstarc = p;
  184.         return true;
  185. }
  186. void CreateGraph(ALGraph &G, int Kind, char v[])
  187. {
  188.         int i, w;
  189.         G.kind = Kind;
  190.         G.arcnum = 0;
  191.         i = 0;
  192.         while (true)
  193.         {
  194.                 if (v[i] == '#')
  195.                 {
  196.                         break;
  197.                 }
  198.                 i++;
  199.                 G.vexs[i].data = v[i - 1];
  200.                 G.vexs[i].firstarc = NULL;
  201.         }
  202.         G.vexnum = i;
  203.         i++;
  204.         while (true)
  205.         {
  206.                 if (v[i] == '#')
  207.                 {
  208.                         break;
  209.                 }
  210.                 if (G.kind == 1 || G.kind == 3)
  211.                 {
  212.                         w = 1;
  213.                 }
  214.                 else
  215.                 {
  216.                         w = v[i + 2] - 48;
  217.                 }
  218.                 InsertArc(G, v[i], v[i + 1], w);
  219.                 i = i + 2;
  220.                 if (w == 2 || w == 4)
  221.                 {
  222.                         i++;
  223.                 }
  224.         }
  225.  
  226. }
  227. void GraphPrint(ALGraph &G)
  228. {
  229.         ArcPtr p;
  230.         cout << "邻接表" << endl;
  231.         for (int i = 1; i <= G.vexnum; i++)
  232.         {
  233.                 cout << G.vexs[i].data;
  234.                 for (p = G.vexs[i].firstarc; p; p = p->nextarc)
  235.                 {
  236.                         cout << "->" << p->adjvex;
  237.                 }
  238.                 cout << endl;
  239.         }
  240. }

回复 "数据结构与算法----4.6 用邻接表储存图,用广度优先搜索策略判断图的2个顶点"

这儿你可以回复上面这条便签

captcha