Commit aea2c041 authored by xep's avatar xep
Browse files

2021.6.9

parent c343f44c
#include <iostream>
#include <cstdio>
#include <utility>
#include <stdint.h>
#include <algorithm>
/// <summary>
......@@ -12,9 +13,8 @@ class Graph {
public:
struct Edge;
struct Vertex {
V vdata;
Edge* adj;
Vertex() :vdata(), adj(nullptr) {}
Vertex() :adj(nullptr) {}
};
struct Edge {
E edata;
......@@ -36,6 +36,8 @@ public:
return const_cast<Graph*>(this)->first_edge(v);
}
Edge* get_edge(int u, int v);
inline int get_size()const {
return size;
}
......@@ -47,6 +49,8 @@ public:
Graph& operator=(const Graph&) = delete;
Graph& operator=(Graph&&) = delete;
void show()const;
protected:
int size;
Vertex* vertices;
......@@ -100,6 +104,17 @@ typename Graph<V, E>::Edge* Graph<V, E>::first_edge(int v)
return vertices[v].adj;
}
template<typename V, typename E>
typename Graph<V, E>::Edge* Graph<V, E>::get_edge(int u, int v)
{
auto* e = first_edge(u);
for (; e; e = e->link) {
if (e->to == v)
return e;
}
return nullptr;
}
template<typename V, typename E>
void Graph<V, E>::get_indegrees(int* indeg) const
{
......@@ -111,4 +126,16 @@ void Graph<V, E>::get_indegrees(int* indeg) const
e = e->link;
}
}
}
template<typename V, typename E>
void Graph<V, E>::show() const
{
for (int v = 0; v < size; v++) {
std::cout << "node: " << v << ", ";
for (auto* e = first_edge(v); e; e = e->link) {
std::cout << "(" << e->to << ", " << e->edata << "), ";
}
std::cout << std::endl;
}
}
\ No newline at end of file
#include <iostream>
#include <utility>
#include <algorithm>
/// <summary>
/// 教材 p.362
......@@ -32,10 +33,18 @@ public:
Edge* add_edge(int from, int to, const E& data);
void remove_edge(int from, int to); //需要时实现
inline const Edge* first_edge(int v)const {
return vertices[v].outed;
}
inline Edge* first_edge(int v) {
return vertices[v].outed;
}
Edge* next_edge(int v, Edge* e);
Edge* next_edge(int v, const Edge* e);
const Edge* next_edge(int v, const Edge* e)const {
return const_cast<UniGraph*>(this)->next_edge(v, e);
}
inline int get_edge_neighbour(int u, const Edge* e)const {
return u == e->v1 ? e->v2 : e->v1;
......@@ -74,7 +83,7 @@ UniGraph<V, E>::~UniGraph() noexcept
{
//先按照出边顺序清除表
for (int i = 0; i < size; i++) {
auto e = vertices[i].outed;
Edge* e = vertices[i].outed;
if (e) {
auto e1 = next_edge(i, e);
while (e1) {
......@@ -110,7 +119,7 @@ UniGraph<V, E>::add_edge(int from, int to, const E& data)
}
template<typename V, typename E>
typename UniGraph<V,E>::Edge* UniGraph<V, E>::next_edge(int v, Edge* e)
typename UniGraph<V, E>::Edge* UniGraph<V, E>::next_edge(int v, const Edge* e)
{
if (v == e->v1) {
return e->path1;
......@@ -128,7 +137,7 @@ typename UniGraph<V, E>::Edge*& UniGraph<V, E>::next_edge_ref(int v, Edge* e)
}
template<typename V, typename E>
typename UniGraph<V,E>::Edge* UniGraph<V, E>::previous_edge(int v, Edge* e)
typename UniGraph<V, E>::Edge* UniGraph<V, E>::previous_edge(int v, Edge* e)
{
if (v == e->v1) {
return e->path2;
......@@ -140,7 +149,7 @@ template<typename V, typename E>
typename UniGraph<V, E>::Edge* UniGraph<V, E>::get_edge(int u, int v)
{
auto* e = first_edge(u);
for (; e; e = next_edge(u,e)) {
for (; e; e = next_edge(u, e)) {
if ((e->v1 == v && e->v2 == u) || (e->v2 == v && e->v1 == u)) {
return e;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment