diff --git a/Algorithm/Recursion/0Resursion 递归 b/Algorithm/BackTracking/回溯 similarity index 100% rename from Algorithm/Recursion/0Resursion 递归 rename to Algorithm/BackTracking/回溯 diff --git a/Algorithm/Divide&Conquer/分而治之 b/Algorithm/Divide&Conquer/分而治之 new file mode 100644 index 0000000..e69de29 diff --git a/Algorithm/Greedy/贪心算法 b/Algorithm/Greedy/贪心算法 new file mode 100644 index 0000000..e69de29 diff --git a/Algorithm/Recursion/P27_All_Sorted b/Algorithm/Recursion/P27_All_Sorted deleted file mode 100644 index b6b02ee..0000000 Binary files a/Algorithm/Recursion/P27_All_Sorted and /dev/null differ diff --git a/Algorithm/Recursion/P28_19_n!.exe b/Algorithm/Recursion/P28_19_n!.exe deleted file mode 100644 index e6d5ae3..0000000 Binary files a/Algorithm/Recursion/P28_19_n!.exe and /dev/null differ diff --git a/Algorithm/Recursion/P28_20_Feb.exe b/Algorithm/Recursion/P28_20_Feb.exe deleted file mode 100644 index b766ac9..0000000 Binary files a/Algorithm/Recursion/P28_20_Feb.exe and /dev/null differ diff --git a/Algorithm/Recursion/P29_21_ComplexFunc.exe b/Algorithm/Recursion/P29_21_ComplexFunc.exe deleted file mode 100644 index 431a577..0000000 Binary files a/Algorithm/Recursion/P29_21_ComplexFunc.exe and /dev/null differ diff --git a/Algorithm/Recursion/P29_22_Ackermann.exe b/Algorithm/Recursion/P29_22_Ackermann.exe deleted file mode 100644 index daf9451..0000000 Binary files a/Algorithm/Recursion/P29_22_Ackermann.exe and /dev/null differ diff --git a/Algorithm/Recursion/P29_23_GCD.exe b/Algorithm/Recursion/P29_23_GCD.exe deleted file mode 100644 index 5d8f6a3..0000000 Binary files a/Algorithm/Recursion/P29_23_GCD.exe and /dev/null differ diff --git a/Algorithm/Recursion/P29_24_checkfunc.exe b/Algorithm/Recursion/P29_24_checkfunc.exe deleted file mode 100644 index adb0b3d..0000000 Binary files a/Algorithm/Recursion/P29_24_checkfunc.exe and /dev/null differ diff --git a/Algorithm/Recursion/P29_25_SubsetGen b/Algorithm/Recursion/P29_25_SubsetGen deleted file mode 100644 index 7b6b31e..0000000 Binary files a/Algorithm/Recursion/P29_25_SubsetGen and /dev/null differ diff --git a/Data-Structure/Graph/adjacencyList/graph.h b/Data-Structure/Graph/adjacencyList/graph.h new file mode 100644 index 0000000..77343fe --- /dev/null +++ b/Data-Structure/Graph/adjacencyList/graph.h @@ -0,0 +1,140 @@ +#pragma once +/* +ɴ0ʼ1ʼСҪ + 1 +*/ +//#include +#include +#include +#include +template +struct edge { + int from, to; + T weight; + + edge(int _from, int _to, const T& _weight) : from(_from), to(_to), weight(_weight) {} +}; +template +class adjacentList { +private: + int v; + int e; + std::deque>> adj;//pair + //ڽӱûҪ-static constexpr T INF = std::limits::infinity(); +public: + std::deque 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* _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 +inline bool adjacentList::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 +inline void adjacentList::insertEdge(edge* _e) +{ + if (!validateV(_e->from) || !validateV(_e->to)) return; + adj[_e->from].push_back(std::make_pair(_e->to, _e->weight)); +} + +template +inline void adjacentList::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᷵µit + } + else { + ++it; + } + } + return; +} + +template +inline int adjacentList::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++; + }*/ + //עд: pair - [to, w] + for (auto [to, w] : adj[i]) { + if (to == _v) js++; + } + } + return js; +} + +template +inline int adjacentList::outDegree(int _v) const +{ + if (!validateV(_v)) return 0; + return adj[_v].size(); +} + +template +inline void adjacentList::bfs(int _v) +{ + if (!validateV(_v)) return; + std::deque que; + std::deque 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 +inline void adjacentList::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); + } + } +} diff --git a/Data-Structure/Graph/adjacencyList/main.cpp b/Data-Structure/Graph/adjacencyList/main.cpp new file mode 100644 index 0000000..5265427 --- /dev/null +++ b/Data-Structure/Graph/adjacencyList/main.cpp @@ -0,0 +1,22 @@ +#include "graph.h" +using namespace std; +int main() { + adjacentList g(11); + edge 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 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 +*/ \ No newline at end of file diff --git a/Data-Structure/结束于2025-8-29 b/Data-Structure/结束于2025-8-29 new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index e3d5b69..f93ad26 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ -# Data-Structure - -数据结构与算法 \ No newline at end of file +# 数据结构与算法-仓库 +## 文件详解 +|文件夹名称|内容|备注| +|---|---|---| +|Data-Structure|数据结构|2025-8-29完成| +|Algorithm|算法|正在制作| +|Features|C++特性|C11/C14/C17/C20| +|std|C++std库|包含STL等多种常用库|