> Vexnum >> Arcnum; //读入顶点数,弧数目; for (int i = 0;i < Vexnum;i++) { char ch;int w; file ...-Go服务器开发" />

ZBLOG

根据读入代码 bool LoadData(ALDirNetwork& graph) { ifstream file("map.txt"); if (!file.is_open()) { return false; } int Vexnum, Arcnum; file >> Vexnum >> Arcnum; //读入顶点数,弧数目; for (int i = 0;i < Vexnum;i++) { char ch;int w; file ...

以下是使用C++编写的不使用STL库的带权无向图类的示例代码:

  1. #include <iostream>
  2. #include <fstream>
  3. class ALDirNetwork {
  4. private:
  5. struct Node {
  6. char vertex;
  7. int weight;
  8. Node* next;
  9. Node(char v, int w) : vertex(v), weight(w), next(nullptr) {}
  10. };
  11. struct Vertex {
  12. char label;
  13. int weight;
  14. Node* head;
  15. Vertex(char l, int w) : label(l), weight(w), head(nullptr) {}
  16. };
  17. Vertex* vertices; // 存储顶点数组
  18. int numVertices; // 顶点数量
  19. public:
  20. ALDirNetwork() : vertices(nullptr), numVertices(0) {}
  21. ~ALDirNetwork() {
  22. if (vertices != nullptr) {
  23. for (int i = 0; i < numVertices; ++i) {
  24. Node* current = vertices[i].head;
  25. while (current != nullptr) {
  26. Node* temp = current->next;
  27. delete current;
  28. current = temp;
  29. }
  30. }
  31. delete[] vertices;
  32. }
  33. }
  34. void InsertVex(char v, int w) {
  35. if (FindVertexIndex(v) == -1) { // 确保顶点不存在时才插入
  36. Vertex* newVertices = new Vertex[numVertices + 1];
  37. for (int i = 0; i < numVertices; ++i)
  38. newVertices[i] = vertices[i];
  39. newVertices[numVertices] = { v, w, nullptr };
  40. ++numVertices;
  41. delete[] vertices;
  42. vertices = newVertices;
  43. }
  44. }
  45. void InsertArc(char v1, char v2, int w) {
  46. int index1 = FindVertexIndex(v1);
  47. int index2 = FindVertexIndex(v2);
  48. if (index1 != -1 && index2 != -1) { // 确保顶点存在才插入边
  49. Node* newNode = new Node(v2, w);
  50. newNode->next = vertices[index1].head;
  51. vertices[index1].head = newNode;
  52. newNode = new Node(v1, w);
  53. newNode->next = vertices[index2].head;
  54. vertices[index2].head = newNode;
  55. }
  56. }
  57. bool LoadData(const char* filename) {
  58. std::ifstream file(filename);
  59. if (!file.is_open()) {
  60. return false;
  61. }
  62. int Vexnum, Arcnum;
  63. file >> Vexnum >> Arcnum; //读入顶点数,弧数目;
  64. for (int i = 0; i < Vexnum; ++i) {
  65. char ch;
  66. int w;
  67. file >> ch >> w;
  68. InsertVex(ch, w); //插入顶点
  69. }
  70. for (int i = 0; i < Arcnum; ++i) {
  71. char v1, v2;
  72. int w;
  73. file >> v1 >> v2 >> w;
  74. InsertArc(v1, v2, w);
  75. }
  76. file.close();
  77. return true;
  78. }
  79. private:
  80. int FindVertexIndex(char label) const {
  81. for (int i = 0; i < numVertices; ++i)
  82. if (vertices[i].label == label)
  83. return i;
  84. return -1; // 找不到返回-1
  85. }
  86. };
  87. int main() {
  88. ALDirNetwork graph;
  89. if (graph.LoadData("map.txt")) {
  90. // 数据加载成功,可以进行后续操作
  91. } else {
  92. // 数据加载失败,处理错误情况
  93. }
  94. return 0;
  95. }

以上代码实现了一个基于邻接链表的带权无向图类ALDirNetwork。该类包含私有成员变量vertices(顶点数组)和numVertices(顶点数量),以及私有嵌套结构体Node(表示边的节点)和Vertex(表示顶点)。通过插入顶点和边的操作,可以构建带权无向图。

在示例代码中,我们使用文件名为”map.txt”来加载数据,并根据需要实现其他功能。请确保文件存在并按正确格式存储数据。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:> Vexnum >> Arcnum; //读入顶点数,弧数目; for (int i = 0;i < Vexnum;i++) { char ch;int w; file ...">https://golang.0voice.com/?id=10569

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?