介绍
无向图的基本操作,增删遍历
Demo
#include <iostream>
#define N 100
typedef struct _GRAPH
{
char vertex[N];
bool edge[N][N];
int vn;
}GRAPH, *PGRAPH;
PGRAPH Create()
{
PGRAPH pGraph = new GRAPH;
memset(pGraph, 0, sizeof(GRAPH));
std::cout << "vertex count:";
std::cin >> pGraph->vn;
for (int i = 0; i < pGraph->vn; i++)
{
int data = 0;
std::cout << "vertex " << i+1 << ": ";
std::cin >> pGraph->vertex[i];
}
for (int i = 0; i < pGraph->vn; i++)
{
for (int j = i + 1; j < pGraph->vn; j++)
{
std::cout << pGraph->vertex[i] << " --- " << pGraph->vertex[j] << ": ";
std::cin >> pGraph->edge[i][j];
pGraph->edge[j][i] = pGraph->edge[i][j];
}
}
return pGraph;
}
void print(PGRAPH pGraph)
{
for (int i = 0; i < pGraph->vn; i++)
{
std::cout << pGraph->vertex[i] << " --- ";
for (int j = 0; j < N; j++)
{
if (pGraph->edge[i][j] == 0)
{
continue;
}
std::cout << pGraph->vertex[j] << " ";
}
std::cout << std::endl;
}
}
bool InsertVertex(PGRAPH pGraph, char vertex)
{
for (int i = 0; i < pGraph->vn; i++)
{
if (pGraph->vertex[i] == vertex)
{
return true;
}
}
pGraph->vertex[pGraph->vn++] = vertex;
return true;
}
bool InsertEdge(PGRAPH pGraph, char src_vertex, char dst_vertex)
{
int src_index = -1;
int dst_index = -1;
for (int i = 0; i < pGraph->vn; i++)
{
if (pGraph->vertex[i] == src_vertex)
{
src_index = i;
}
if (pGraph->vertex[i] == dst_vertex)
{
dst_index = i;
}
if (src_index != -1 && dst_index != -1)
{
break;
}
}
if (src_index == -1 || dst_index == -1)
{
return false;
}
pGraph->edge[src_index][dst_index] = 1;
pGraph->edge[dst_index][src_index] = 1;
return true;
}
bool DeleteVertex(PGRAPH pGraph, char vertex)
{
for (int i = 0; i < pGraph->vn; i++)
{
if (pGraph->vertex[i] == vertex)
{
for (int j = 0; j < pGraph->vn; j++)
{
if (pGraph->edge[i][j] != 0)
{
pGraph->edge[j][i] = 0;
pGraph->edge[i][j] = 0;
}
}
pGraph->vertex[pGraph->vn] = '\0';
for (int j = i; j < pGraph->vn - 1; j++)
{
pGraph->vertex[j] = pGraph->vertex[j + 1];
}
pGraph->vn--;
return true;
}
}
return true;
}
bool DeleteEdge(PGRAPH pGraph, char src_vertex, char dst_vertex)
{
int src_index = -1;
int dst_index = -1;
for (int i = 0; i < pGraph->vn; i++)
{
if (pGraph->vertex[i] == src_vertex)
{
src_index = i;
}
if (pGraph->vertex[i] == dst_vertex)
{
dst_index = i;
}
if (src_index != -1 && dst_index != -1)
{
break;
}
}
if (src_index == -1 || dst_index == -1)
{
return false;
}
pGraph->edge[src_index][dst_index] = 0;
pGraph->edge[dst_index][src_index] = 0;
return true;
}
int main()
{
PGRAPH pGraph = Create();
print(pGraph);
std::cout << "InsertEdge a-e after: " << std::endl;
InsertEdge(pGraph, 'a', 'e');
print(pGraph);
std::cout << "InsertVertex f after: " << std::endl;
InsertVertex(pGraph, 'f');
print(pGraph);
std::cout << "InsertEdge f-a after: " << std::endl;
InsertEdge(pGraph, 'f', 'a');
print(pGraph);
std::cout << "DeleteEdge f-a after: " << std::endl;
DeleteEdge(pGraph, 'f', 'a');
print(pGraph);
std::cout << "DeleteVertex f after: " << std::endl;
DeleteVertex(pGraph, 'f');
print(pGraph);
return 0;
}
输入
5
a
b
c
d
e
1
1
1
0
1
1
0
1
1
0
输出
vertex count:5
vertex 1: a
vertex 2: b
vertex 3: c
vertex 4: d
vertex 5: e
a --- b: 1
a --- c: 1
a --- d: 1
a --- e: 0
b --- c: 1
b --- d: 1
b --- e: 0
c --- d: 1
c --- e: 1
d --- e: 0
a --- b c d
b --- a c d
c --- a b d e
d --- a b c
e --- c
InsertEdge a-e after:
a --- b c d e
b --- a c d
c --- a b d e
d --- a b c
e --- a c
InsertVertex f after:
a --- b c d e
b --- a c d
c --- a b d e
d --- a b c
e --- a c
f ---
InsertEdge f-a after:
a --- b c d e f
b --- a c d
c --- a b d e
d --- a b c
e --- a c
f --- a
DeleteEdge f-a after:
a --- b c d e
b --- a c d
c --- a b d e
d --- a b c
e --- a c
f ---
DeleteVertex f after:
a --- b c d e
b --- a c d
c --- a b d e
d --- a b c
e --- a c