还有bug

This commit is contained in:
iridiumR 2021-12-10 09:20:41 +08:00
parent 38a5007775
commit 827e98e87a
3 changed files with 42 additions and 7 deletions

View file

@ -4,12 +4,17 @@
int main()
{
// typedef struct data
// {
// int id;
// std::string name;
// }data;
graphVertex<int, std::string> 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;
}

View file

@ -1,6 +1,7 @@
#ifndef _graph_hpp_
#define _graph_hpp_
#include "vector.hpp"
#include <iomanip>
#include <iostream>
#include <string>
// #define gNodeArray(T) binNode<T> *
@ -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<Te> *temp = new Edge<Te>(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

View file

@ -32,11 +32,15 @@ public:
_used = 0;
_len = 100;
_v = new T[_len];
}
~Vector()
for (int i = 0; i < _len; i++)
{
delete[] _v;
_v[i] = 0;
}
}
// ~Vector()
// {
// delete[] _v;
// }
T get(int a) { return _v[a]; }
T operator[](int i) const