还有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() int main()
{ {
// typedef struct data
// {
// int id;
// std::string name;
// }data;
graphVertex<int, std::string> gv; graphVertex<int, std::string> gv;
int op, id1, id2, w; int op, id1, id2, w;
std::string name; std::string name;
while (1) while (1)
{ {
printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边\n"); printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边5显示图\n");
scanf("%d", &op); scanf("%d", &op);
switch (op) switch (op)
{ {
@ -32,6 +37,8 @@ int main()
std::cin >> id1 >> id2; std::cin >> id1 >> id2;
gv.unlink(id1, id2); gv.unlink(id1, id2);
break; break;
case 5:
gv.display();
default: default:
break; break;
} }

View file

@ -1,6 +1,7 @@
#ifndef _graph_hpp_ #ifndef _graph_hpp_
#define _graph_hpp_ #define _graph_hpp_
#include "vector.hpp" #include "vector.hpp"
#include <iomanip>
#include <iostream> #include <iostream>
#include <string> #include <string>
// #define gNodeArray(T) binNode<T> * // #define gNodeArray(T) binNode<T> *
@ -100,7 +101,7 @@ public:
{ {
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);
@ -127,8 +128,9 @@ public:
return -2; return -2;
V[id1]->outDegree++; V[id1]->outDegree++;
V[id2]->inDegree++; V[id2]->inDegree++;
e++;
Edge<Te> *temp = new Edge<Te>(w); Edge<Te> *temp = new Edge<Te>(w);
(E._v[id1])._v[id2] = temp; (E[id1]).put(id2, temp);
return 0; return 0;
} }
// 解除 id1->id2 连接 // 解除 id1->id2 连接
@ -148,9 +150,31 @@ public:
V[id2]->inDegree--; V[id2]->inDegree--;
delete E[id1][id2]; delete E[id1][id2];
e--;
return 0; 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 #endif

View file

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