This commit is contained in:
iridiumR 2021-12-10 00:34:32 +08:00
parent d4c6ba0eb9
commit 6099cd16c8
3 changed files with 29 additions and 31 deletions

View file

@ -9,7 +9,7 @@ int main()
std::string name; std::string name;
while (1) while (1)
{ {
printf("选择操作:1添加节点2添加边3删除节点4删除边\n"); printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边\n");
scanf("%d", &op); scanf("%d", &op);
switch (op) switch (op)
{ {

View file

@ -53,7 +53,7 @@ class graphVertex
{ {
private: private:
Vector<Vertex<Tv> *> V; Vector<Vertex<Tv> *> V;
Vector<Vector<Edge<Te> *>> E; Vector<Edge<Te> *> E;
int v; int v;
int e; int e;
@ -86,25 +86,24 @@ public:
} }
virtual int insect(int id, Tv value) virtual int insect(int id, Tv value)
{ {
if (V[id] == NULL) // if (V[id] == NULL)
{ // {
Vertex<Tv>* temp= new Vertex<Tv>(value); Vertex<Tv> *temp1 = new Vertex<Tv>(value);
V.put(id, temp); V.put(id, temp1);
// E[id] = new Vector<Edge<Te>>; // }
} // else
else // V[id]->data = value;
V[id]->data = value;
v++; v++;
return id; return id;
} }
virtual int remove(int id) virtual Tv remove(int id)
{ {
for (int j = 0; j < v; j++) for (int j = 0; j < v; j++)
{ {
if (E[id][j] != NULL) if (E[id][j]!= NULL)
{ {
delete E[id][j]; delete E[id][j];
E[id].put(j,NULL); E[id].put(j, NULL);
V[j]->inDegree--; V[j]->inDegree--;
} }
} }
@ -112,6 +111,7 @@ public:
Tv temp = vertex(id); Tv temp = vertex(id);
delete V[id]; delete V[id];
V.put(id, NULL); V.put(id, NULL);
return temp;
} }
// id1->id2 进行连接 // id1->id2 进行连接
@ -121,14 +121,14 @@ public:
// 0 成功 // 0 成功
virtual int link(int id1, int id2, int w) virtual int link(int id1, int id2, int w)
{ {
if (V[id1] == NULL || V[id2] == NULL) // if (V[id1] == NULL || V[id2] == NULL)
return -1; // return -1;
if (E[id1][id2] != NULL) // if (E[id1][id2] != NULL)
return -2; // return -2;
V[id1]->outDegree++; V[id1]->outDegree++;
V[id2]->inDegree++; V[id2]->inDegree++;
Edge<Te>* temp=new Edge<Te>(w); Edge<Te> *temp = new Edge<Te>(w);
E[id1].put(id2, temp); (E->_v[id1])->_v[id2] = temp;
return 0; return 0;
} }
// 解除 id1->id2 连接 // 解除 id1->id2 连接

View file

@ -6,7 +6,7 @@
template <class T> template <class T>
class Vector class Vector
{ {
private: public:
T *_v; T *_v;
int _len; int _len;
int _used; int _used;
@ -30,7 +30,7 @@ public:
Vector() Vector()
{ {
_used = 0; _used = 0;
_len = 10; _len = 100;
_v = new T[_len]; _v = new T[_len];
} }
~Vector() ~Vector()
@ -39,12 +39,10 @@ public:
} }
T get(int a) { return _v[a]; } T get(int a) { return _v[a]; }
T operator[](int i) T operator[](int i) const
{ {
while (i >= _len) if(i>=_len)
{ return 0;
expand();
}
return _v[i]; return _v[i];
} }
@ -84,10 +82,10 @@ template <class T>
void Vector<T>::put(int a, T value) void Vector<T>::put(int a, T value)
{ {
while (a >= _len) // while (a >= _len)
{ // {
expand(); // expand();
} // }
_used++; _used++;
_v[a] = value; _v[a] = value;
} }
@ -110,7 +108,7 @@ void Vector<T>::expand()
for (int i = old_len; i < _len; i++) for (int i = old_len; i < _len; i++)
{ {
p[i] = NULL; p[i] = 0;
} }
delete[] _v; delete[] _v;