【C++数据结构】链表实现图的存储及拓扑排序,基于C++自带的栈、队列及递归实现的拓扑排序。

链表实现的图存储及拓扑排序

注:本文内容为图的简易存储及拓扑排序,
标准版请查看[链表实现图存储及其遍历]

如下图所示,本文代码主要实现c图的结构,并基于该结构完成广度和深度优先排序。
结构示意图
代码如下:

#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<list>
#include<stdlib.h>
using namespace std;
typedef int Vertex;
template<int graph_size>
class Diagraph {
   
public:
	Diagraph();
	void read();
	void write();
	void depth_sort(list<Vertex> &topological_order);//深度优先递归拓扑排序
	void depth_sort_stack(list<Vertex> &topological_order);//深度优先拓扑排序栈
	void breadth_sort(list<Vertex> &topological_order);//广度优先拓扑排序
private:
	int count;
	list<Vertex> neighbors[graph_size];    //存放每个节点的后继
	void recursive_depth_sort(Vertex v, bool visited[], list<Vertex> &topological_order);//递归排序
};


template<int graph_size> Diagraph<graph_size>::Diagraph() {
   
	count = 0;
}
template<int graph_size>void Diagraph<graph_size>::read() {
   
	cin >> count;
	for (Vertex v = 0; v < count; v++) {
      //输入count个节点
		int degree = 0;
		string c;
		cout << "Vertex" << v << ":" << endl;
		while (1) {
   
			cin >> c;                //输入后继节点
			if (c[0] == '-')   //输入-该节点输入结束
				break;
			int w = atoi(c.c_str());   //字符串转为int型
			neighbors[v].push_back(w);
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

輕塵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值