diff --git a/ex11/ex11.cpp b/ex11/ex11.cpp index 9ae0181..cb864bb 100644 --- a/ex11/ex11.cpp +++ b/ex11/ex11.cpp @@ -9,7 +9,7 @@ int main() std::string name; while (1) { - printf("选择操作:1添加节点2添加边3删除节点4删除边\n"); + printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边\n"); scanf("%d", &op); switch (op) { diff --git a/ex11/graph.hpp b/ex11/graph.hpp index 8577c00..aeb5fbb 100644 --- a/ex11/graph.hpp +++ b/ex11/graph.hpp @@ -53,7 +53,7 @@ class graphVertex { private: Vector *> V; - Vector *>> E; + Vector *> E; int v; int e; @@ -86,25 +86,24 @@ public: } virtual int insect(int id, Tv value) { - if (V[id] == NULL) - { - Vertex* temp= new Vertex(value); - V.put(id, temp); - // E[id] = new Vector>; - } - else - V[id]->data = value; + // if (V[id] == NULL) + // { + Vertex *temp1 = new Vertex(value); + V.put(id, temp1); + // } + // else + // V[id]->data = value; v++; return id; } - virtual int remove(int id) + virtual Tv remove(int id) { 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); + E[id].put(j, NULL); V[j]->inDegree--; } } @@ -112,6 +111,7 @@ public: Tv temp = vertex(id); delete V[id]; V.put(id, NULL); + return temp; } // id1->id2 进行连接 @@ -121,14 +121,14 @@ public: // 0 成功 virtual int link(int id1, int id2, int w) { - if (V[id1] == NULL || V[id2] == NULL) - return -1; - if (E[id1][id2] != NULL) - return -2; + // if (V[id1] == NULL || V[id2] == NULL) + // return -1; + // if (E[id1][id2] != NULL) + // return -2; V[id1]->outDegree++; V[id2]->inDegree++; - Edge* temp=new Edge(w); - E[id1].put(id2, temp); + Edge *temp = new Edge(w); + (E->_v[id1])->_v[id2] = temp; return 0; } // 解除 id1->id2 连接 diff --git a/ex11/vector.hpp b/ex11/vector.hpp index cb138bd..181c347 100644 --- a/ex11/vector.hpp +++ b/ex11/vector.hpp @@ -6,7 +6,7 @@ template class Vector { -private: +public: T *_v; int _len; int _used; @@ -30,7 +30,7 @@ public: Vector() { _used = 0; - _len = 10; + _len = 100; _v = new T[_len]; } ~Vector() @@ -39,12 +39,10 @@ public: } T get(int a) { return _v[a]; } - T operator[](int i) + T operator[](int i) const { - while (i >= _len) - { - expand(); - } + if(i>=_len) + return 0; return _v[i]; } @@ -84,10 +82,10 @@ template void Vector::put(int a, T value) { - while (a >= _len) - { - expand(); - } + // while (a >= _len) + // { + // expand(); + // } _used++; _v[a] = value; } @@ -110,7 +108,7 @@ void Vector::expand() for (int i = old_len; i < _len; i++) { - p[i] = NULL; + p[i] = 0; } delete[] _v;