目录
一)注意事项
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;
}