数据结构-无权无向图

本文档演示了如何使用C++实现无向图的创建、插入顶点和边、删除顶点和边,以及基本的遍历操作。通过实例展示了`GRAPH`结构和相关函数如`Create()`、`InsertVertex()`、`InsertEdge()`等的用法。
摘要由CSDN通过智能技术生成

数据结构-无权无向图

介绍

无向图的基本操作,增删遍历

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值