似乎能用了

This commit is contained in:
iridiumR 2021-12-10 16:51:43 +08:00
parent 780a537e50
commit edd925d4fc
3 changed files with 47 additions and 14 deletions

View file

@ -14,28 +14,51 @@ int main()
std::string name;
while (1)
{
printf("选择操作:1添加/覆盖节点2添加/覆盖边3删除节点4删除边5显示图\n");
printf("选择操作:1添加/覆盖节点2添加边3删除节点4删除边5显示图\n");
scanf("%d", &op);
switch (op)
{
case 1:
printf("分别输入序号 节点名:");
std::cin >> id1 >> name;
gv.insect(id1, name);
printf("插入到ID位%d的位置\n", gv.insect(id1, name));
break;
case 2:
printf("分别输入起始节点序号 到达节点序号 边权重:");
std::cin >> id1 >> id2 >> w;
gv.link(id1, id2, w);
switch (gv.link(id1, id2, w))
{
case 0:
printf("成功\n");
break;
case -1:
printf("顶点不存在\n");
break;
case -2:
printf("边已经存在\n");
break;
}
break;
case 3:
printf("输入节点序号:");
std::cin >> id1;
gv.remove(id1);
break;
case 4:
printf("分别输入起始节点序号 到达节点序号:");
std::cin >> id1 >> id2;
gv.unlink(id1, id2);
switch (gv.unlink(id1, id2))
{
case 0:
printf("成功\n");
break;
case -1:
printf("顶点不存在\n");
break;
case -2:
printf("边不存在\n");
break;
}
break;
case 5:
gv.display();

View file

@ -4,7 +4,7 @@
#include <iomanip>
#include <iostream>
#include <string>
// #define gNodeArray(T) binNode<T> *
template <class Te>
class Edge
@ -54,7 +54,8 @@ class graphVertex
{
private:
Vector<Vertex<Tv> *> V;
Vector<Vector<Edge<Te> *>> E;
Edge<Te> *E[100][100] = {NULL};
int v;
int e;
@ -99,12 +100,14 @@ public:
}
virtual Tv remove(int id)
{
if(V[id]==NULL)
return (Tv)0;
for (int j = 0; j < v; j++)
{
if (E[id][j] != NULL)
{
delete E[id][j];
E[id].put(j, NULL);
E[id][j] = NULL;
V[j]->inDegree--;
}
}
@ -130,7 +133,7 @@ public:
V[id2]->inDegree++;
e++;
Edge<Te> *temp = new Edge<Te>(w);
(E[id1]).put(id2, temp);
E[id1][id2]= temp;
return 0;
}
// 解除 id1->id2 连接
@ -143,7 +146,7 @@ public:
if (V[id1] == NULL || V[id2] == NULL)
return -1;
if (E[id1][id2] != NULL)
if (E[id1][id2] == NULL)
return -2;
V[id1]->outDegree--;
@ -156,23 +159,30 @@ public:
}
virtual void display()
{
{
// std::cout << std::setw(14) << temp->weight;
//第一行
std::cout << std::left << std::setw(14) << " ";
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 < V._len; j++)
if (V[j] != NULL)
{
if (E[i][j] == NULL)
std::cout << std::setw(14) << "-";
else
if (E[i][j] != NULL)
std::cout << std::setw(14) << E[i][j]->weight;
else
std::cout << std::setw(14) << "-";
}
printf("\n");
}

View file

@ -30,7 +30,7 @@ public:
Vector()
{
_used = 0;
_len = 100;
_len = 10;
_v = new T[_len];
for (int i = 0; i < _len; i++)
{