DS-Completed

This commit is contained in:
e2hang
2025-08-29 08:42:50 +08:00
parent 1a5c4329b2
commit 9077007670
15 changed files with 170 additions and 3 deletions

View File

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,140 @@
#pragma once
/*
<EFBFBD><EFBFBD><EFBFBD>ɴ<EFBFBD>0<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>Ҫ + 1
*/
//#include <limits>
#include <deque>
#include <utility>
#include <iostream>
template <class T>
struct edge {
int from, to;
T weight;
edge(int _from, int _to, const T& _weight) : from(_from), to(_to), weight(_weight) {}
};
template <class T>
class adjacentList {
private:
int v;
int e;
std::deque<std::deque<std::pair<int, T>>> adj;//pair<to, weight>
//<2F>ڽӱ<DABD>û<EFBFBD><C3BB>Ҫ<EFBFBD><D2AA>-static constexpr T INF = std::limits<T>::infinity();
public:
std::deque<bool> reach;
adjacentList() = delete;
adjacentList(int _v) : v(_v), e(0) {
adj.resize(v);
reach.assign(v, false);
}
~adjacentList() = default;
bool validateV(int _v) const {
return _v >= 0 && _v < v;
}
int numberOfVertices() const { return v; }
int numberOfEdges() const { return e; }
bool existsEdge(int _a, int _b) const;
void insertEdge(edge<T>* _e);
void eraseEdge(int _a, int _b);
int degree(int _v) const { return inDegree(_v) + outDegree(_v); }
int inDegree(int _v) const;
int outDegree(int _v) const;
void bfs(int _v);
void dfs(int _v);
};
template<class T>
inline bool adjacentList<T>::existsEdge(int _a, int _b) const
{
if (!validateV(_a) || !validateV(_b)) return false;
for (auto x : adj[_a]) {
if (x.first == _b) return true;
}
return false;
}
template<class T>
inline void adjacentList<T>::insertEdge(edge<T>* _e)
{
if (!validateV(_e->from) || !validateV(_e->to)) return;
adj[_e->from].push_back(std::make_pair(_e->to, _e->weight));
}
template<class T>
inline void adjacentList<T>::eraseEdge(int _a, int _b)
{
if (!validateV(_a) || !validateV(_b)) return;
for (auto it = adj[_a].begin(); it != adj[_a].end();) {
if ((*it).first == _b) {
it = adj[_a].erase(it);//erase<73><EFBFBD><E1B7B5><EFBFBD>µ<EFBFBD>it<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else {
++it;
}
}
return;
}
template<class T>
inline int adjacentList<T>::inDegree(int _v) const
{
if (!validateV(_v)) return 0;
int js = 0;
for (int i = 0; i < v; i++) {
/*for (auto x : adj[i]) {
if (x.first == _v) js++;
}*/
//<2F><>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>: pair - [to, w]
for (auto [to, w] : adj[i]) {
if (to == _v) js++;
}
}
return js;
}
template<class T>
inline int adjacentList<T>::outDegree(int _v) const
{
if (!validateV(_v)) return 0;
return adj[_v].size();
}
template<class T>
inline void adjacentList<T>::bfs(int _v)
{
if (!validateV(_v)) return;
std::deque<int> que;
std::deque<bool> visited(v, false);
que.push_back(_v);
visited[_v] = true;
while (!que.empty()) {
int u = que.front();
que.pop_front();
std::cout << u << " ";
for (auto [to, w] : adj[u]) {
if (visited[to] == false) {
que.push_back(to);
visited[to] = true;
}
}
}
std::cout << std::endl;
}
template<class T>
inline void adjacentList<T>::dfs(int _v)
{
if (!validateV(_v)) return;
reach[_v] = true;
std::cout << _v << " ";
for (auto [to, w] : adj[_v]) {
if (!reach[to]) {
//reach[to] = true;
dfs(to);
}
}
}

View File

@@ -0,0 +1,22 @@
#include "graph.h"
using namespace std;
int main() {
adjacentList<double> g(11);
edge<double> e1(1, 2, 0.5), e2(1, 3, 0.6), e3(1, 4, 0.7), e4(1, 5, 0.8), e5(3, 10, 0.9);
edge<double> e6(10, 7, 0.5), e7(2, 5, 0.6), e8(5, 8, 0.7);
g.insertEdge(&e1);g.insertEdge(&e2);g.insertEdge(&e3);g.insertEdge(&e4);g.insertEdge(&e5);g.insertEdge(&e6);g.insertEdge(&e7);g.insertEdge(&e8);
cout << g.degree(1) << endl;
cout << g.inDegree(5) << endl;
cout << g.outDegree(8) << endl;
g.bfs(1);
g.dfs(1);
return 0;
}
/*
4
2
0
1 2 3 4 5 10 8 7
1 2 5 8 3 10 7 4
*/

View File

View File

@@ -1,3 +1,8 @@
# Data-Structure # 数据结构与算法-仓库
## 文件详解
数据结构与算法 |文件夹名称|内容|备注|
|---|---|---|
|Data-Structure|数据结构|2025-8-29完成|
|Algorithm|算法|正在制作|
|Features|C++特性|C11/C14/C17/C20|
|std|C++std库|包含STL等多种常用库|