diff --git a/ex11/ex11.cpp b/ex11/ex11.cpp index cb864bb..d237bd8 100644 --- a/ex11/ex11.cpp +++ b/ex11/ex11.cpp @@ -4,12 +4,17 @@ int main() { + // typedef struct data + // { + // int id; + // std::string name; + // }data; graphVertex gv; int op, id1, id2, w; std::string name; while (1) { - printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边\n"); + printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边5显示图\n"); scanf("%d", &op); switch (op) { @@ -32,6 +37,8 @@ int main() std::cin >> id1 >> id2; gv.unlink(id1, id2); break; + case 5: + gv.display(); default: break; } diff --git a/ex11/graph.hpp b/ex11/graph.hpp index 089e281..43afa3b 100644 --- a/ex11/graph.hpp +++ b/ex11/graph.hpp @@ -1,6 +1,7 @@ #ifndef _graph_hpp_ #define _graph_hpp_ #include "vector.hpp" +#include #include #include // #define gNodeArray(T) binNode * @@ -100,7 +101,7 @@ public: { for (int j = 0; j < v; j++) { - if (E[id][j]!= NULL) + if (E[id][j] != NULL) { delete E[id][j]; E[id].put(j, NULL); @@ -127,8 +128,9 @@ public: return -2; V[id1]->outDegree++; V[id2]->inDegree++; + e++; Edge *temp = new Edge(w); - (E._v[id1])._v[id2] = temp; + (E[id1]).put(id2, temp); return 0; } // 解除 id1->id2 连接 @@ -148,9 +150,31 @@ public: V[id2]->inDegree--; delete E[id1][id2]; + e--; return 0; } + + virtual void display() + { + std::cout << std::left << std::setw(5) << " "; + for (int i = 0; i < V._len; i++) + if (V[i] != NULL) + std::cout << std::setw(4) << i << std::setw(10) << V[i]->data; + printf("\n"); + for (int i = 0; i < V._len; i++) + if (V[i] != NULL) + { + std::cout << std::setw(4) << i << std::setw(10) << V[i]->data; + for (int j = 0; j < E[i]._len;j++) + if(E[i][j]==NULL) + std::cout << std::setw(14) << "-"; + else + std::cout<< std::setw(14) << E[i][j]->weight; + printf("\n"); + } + + } }; #endif \ No newline at end of file diff --git a/ex11/vector.hpp b/ex11/vector.hpp index 181c347..91498cc 100644 --- a/ex11/vector.hpp +++ b/ex11/vector.hpp @@ -32,11 +32,15 @@ public: _used = 0; _len = 100; _v = new T[_len]; + for (int i = 0; i < _len; i++) + { + _v[i] = 0; + } } - ~Vector() - { - delete[] _v; - } + // ~Vector() + // { + // delete[] _v; + // } T get(int a) { return _v[a]; } T operator[](int i) const