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;
while (1)
{
printf("选择操作:1添加节点2添加边3删除节点4删除边\n");
printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边\n");
scanf("%d", &op);
switch (op)
{

View file

@ -53,7 +53,7 @@ class graphVertex
{
private:
Vector<Vertex<Tv> *> V;
Vector<Vector<Edge<Te> *>> E;
Vector<Edge<Te> *> E;
int v;
int e;
@ -86,25 +86,24 @@ public:
}
virtual int insect(int id, Tv value)
{
if (V[id] == NULL)
{
Vertex<Tv>* temp= new Vertex<Tv>(value);
V.put(id, temp);
// E[id] = new Vector<Edge<Te>>;
}
else
V[id]->data = value;
// if (V[id] == NULL)
// {
Vertex<Tv> *temp1 = new Vertex<Tv>(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<Te>* temp=new Edge<Te>(w);
E[id1].put(id2, temp);
Edge<Te> *temp = new Edge<Te>(w);
(E->_v[id1])->_v[id2] = temp;
return 0;
}
// 解除 id1->id2 连接

View file

@ -6,7 +6,7 @@
template <class T>
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 <class T>
void Vector<T>::put(int a, T value)
{
while (a >= _len)
{
expand();
}
// while (a >= _len)
// {
// expand();
// }
_used++;
_v[a] = value;
}
@ -110,7 +108,7 @@ void Vector<T>::expand()
for (int i = old_len; i < _len; i++)
{
p[i] = NULL;
p[i] = 0;
}
delete[] _v;