c++图的邻接链表实现(模板)(深度·广度遍历)(插入·尾插法)

目录

一)注意事项

二)代码


一)注意事项

1.尾插法需要一个尾指针,不然插入复杂度会达到O(n)

2.可以使用**动态申请二维数组(具体见:CSDN

二)代码

#include<iostream>
#include<queue>
#include<stack>
#define null NULL
using namespace std;
template<class T>
class Edge {
public:
	T index;
	Edge<T>* next;
	Edge(){}
	Edge(T data) {
		this->index = data;
		this->next = null;
	}
};
template<class T>
class Vertex {
public:
	T index;
	Edge<T>* first;
	Vertex(){}
	Vertex(T data) {
		this->index = data;
		this->first = null;
	}
};
template<class T>
class Graph {
	bool *visited;
	int length;
	Edge<T>** edges;
/*
	1. *edges表示指向Edge的指针,那**edges就是指向 指向Edge指针的指针,而指针又可以
	动态申请数组(譬如:int *a=new int[10]),所以**edges可以动态申请自定义对象数组
	2. 因为我才用的是尾插法,所以需要edges作为尾指针,方便插入边结点,若无,则插入
	需要O(n);【哎,太傻了,用头插法就没这么麻烦了】
*/
public:
	Graph() {}
	Graph(int num) {
		this->length = num;
		this->visited = new bool[num];
		for (int i = 0; i < num; i++)visited[i] = false;
		this->edges = new Edge<T>*[num] {};
		for (int i = 0; i < num; i++) {
			edges[i] = new Edge<T>(i);
		}
	}
	void Initialization_visited() {
		for (int i = 0; i < length; i++)visited[i] = false;
	}
	void add_edge(T data, Vertex<T>*& vertex) {
		if (vertex->first == null) {
			vertex->first = new Edge<T>(data);
			this->edges[vertex->index] = vertex->first;
		}
		else {
			this->edges[vertex->index]->next = new Edge<T>(data);
			this->edges[vertex->index] = this->edges[vertex->index]->next;
		}
	}
	void visit_breadth(Vertex<T>* vertices[]) {
		for (int i = 0; i < length; i++) {
			if (!visited[i]) {
				this->breadth_first_search(vertices[i],vertices);
			}
		}
		this->Initialization_visited();
		cout << endl;
	}
	void breadth_first_search(Vertex<T>*vertex, Vertex<T>* vertices[]) {
		visited[vertex->index] = true;
		queue<Vertex<T>*>myqueue;
		myqueue.push(vertex);
		while (!myqueue.empty()) {
			Vertex<T>* vnode = myqueue.front();
			cout << vnode->index << " ";
			myqueue.pop();
			Edge<T>* enode = vnode->first;
			while (enode != null) {
				if (!visited[enode->index]) {
					myqueue.push(vertices[enode->index]);
					visited[vertices[enode->index]->index] = true;
				}
				enode = enode->next;
			}
		}
	}
	void visit_depth(Vertex<T>* vertices[]) {
		for (int i = 0; i < length; i++) {
			if (!visited[i]) {
				this->depth_first_search(vertices[i], vertices);
			}
		}
		cout << endl;
	}
	void depth_first_search(Vertex<T>* vertex, Vertex<T>* vertices[]) {
		stack<Vertex<T>*>my_stack;
		my_stack.push(vertex);
		visited[vertex->index] = true;
		while (!my_stack.empty()) {
			Vertex<T>* vnode = my_stack.top();
			Edge<T>* enode = vnode->first;
			while (enode != null && visited[enode->index])enode = enode->next;
			if (enode!=null) {
				my_stack.push(vertices[enode->index]);
				visited[enode->index] = true;
			}
			else {
				Vertex<T>* temp = my_stack.top();
				cout << temp->index << " ";
				my_stack.pop();
			}
		}
	}
};
int main() {
	Vertex<int>* vertices[5]{
		new Vertex<int>(0),
		new Vertex<int>(1),
		new Vertex<int>(2),
		new Vertex<int>(3),
		new Vertex<int>(4)
	};
	Graph<int>*graph = new Graph<int>(5);
	graph->add_edge(4, vertices[0]);
	graph->add_edge(1, vertices[0]);
	graph->add_edge(3, vertices[0]);
	graph->add_edge(0, vertices[2]);
	graph->add_edge(3, vertices[2]);
	graph->add_edge(3, vertices[4]);
	graph->add_edge(2, vertices[4]);
	graph->visit_breadth(vertices);
	graph->visit_depth(vertices);
	cout << endl;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值