mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-11 11:07:27 +08:00
Documentation for 842c4ab5dd
This commit is contained in:
@@ -152,7 +152,7 @@ Functions</h2></td></tr>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p>Implementation of <a href="https://simple.wikipedia.org/wiki/Linked_list" target="_blank">Reversing a single linked list</a> </p>
|
||||
<p>The linked list is a data structure used for holding a sequence of values, which can be added, displayed, reversed, or removed. </p>
|
||||
<h3><a class="anchor" id="autotoc_md43"></a>
|
||||
<h3><a class="anchor" id="autotoc_md44"></a>
|
||||
Algorithm</h3>
|
||||
<p>Values can be added by iterating to the end of a list (by following the pointers) starting from the first link. Whichever link points to null is considered the last link and is pointed to the new value.</p>
|
||||
<p>Linked List can be reversed by using 3 pointers: current, previous, and next_node; we keep iterating until the last node. Meanwhile, before changing to the next of current, we store it in the next_node pointer, now we store the prev pointer in the current of next, this is where the actual reversal happens. And then we move the prev and current pointers one step forward. Then the head node is made to point to the last node (prev pointer) after completion of an iteration.</p>
|
||||
|
||||
@@ -142,7 +142,7 @@ Functions</h2></td></tr>
|
||||
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
||||
<div class="textblock"><p>Implementation of cutting a rod problem. </p>
|
||||
<p>Given a rod of length n inches and an array of prices that contains prices of all pieces of size<=n. Determine the maximum profit obtainable by cutting up the rod and selling the pieces.</p>
|
||||
<h3><a class="anchor" id="autotoc_md71"></a>
|
||||
<h3><a class="anchor" id="autotoc_md72"></a>
|
||||
Algorithm</h3>
|
||||
<p>The idea is to break the given rod into every smaller piece as possible and then check profit for each piece, by calculating maximum profit for smaller pieces we will build the solution for larger pieces in bottom-up manner.</p>
|
||||
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/Anmol3299" target="_blank">Anmol</a> </dd>
|
||||
|
||||
@@ -131,7 +131,7 @@ Contributor</h2>
|
||||
- Make sure the file extensions are <tt>*.hpp</tt>, <tt>*.h</tt> or <tt>*.cpp</tt>.
|
||||
- Don't use **<tt>bits/stdc++.h</tt>** because this is quite Linux-specific and slows down the compilation process.
|
||||
- Organize your code using **<tt>struct</tt>**, **<tt>class</tt>**, and/or **<tt>namespace</tt>** keywords.
|
||||
- If an implementation of the algorithm already exists, please refer to the @ref new-file-name-guidelines "file-name section below".
|
||||
- If an implementation of the algorithm already exists, please refer to the @ref file-name-guidelines "file-name section below".
|
||||
- You can suggest reasonable changes to existing algorithms.
|
||||
- Strictly use snake_case (underscore_separated) in filenames.
|
||||
- If you have added or modified code, please make sure the code compiles before submitting.
|
||||
@@ -152,13 +152,85 @@ Contributor</h2>
|
||||
- Make sure to add examples and test cases in your <tt>main()</tt> function.
|
||||
- If you find an algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes.
|
||||
- Please try to add one or more <tt>test()</tt> functions that will invoke the algorithm implementation on random test data with the expected output. Use the <tt>assert()</tt> function to confirm that the tests will pass. Requires including the <tt>cassert</tt> library.
|
||||
- Test cases should fully verify that your program works as expected. Rather than asking the user for input, it's best to make sure the given output is the correct output.
|
||||
|
||||
@subsubsection autotoc_md30 Typical structure of a program
|
||||
@paragraph autotoc_md30 Self-test examples
|
||||
|
||||
1. <a href="<a href="https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp#L137">https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp#L137</a>" target="_blank" >Quick sort</a> testing (complex).
|
||||
|
||||
@icode{cpp}
|
||||
// Let's make sure the array of numbers is ordered after calling the function.
|
||||
std::vector<uint64_t> arr = {5, 3, 8, 12, 14, 16, 28, 96, 2, 5977};
|
||||
std::vector<uint64_t> arr_sorted = sorting::quick_sort::quick_sort(
|
||||
arr, 0, int(std::end(arr) - std::begin(arr)) - 1);
|
||||
|
||||
assert(std::is_sorted(std::begin(arr_sorted), std::end(arr_sorted)));
|
||||
@endicode
|
||||
|
||||
2. <a href="<a href="https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp#L58">https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp#L58</a>" target="_blank" >Subset Sum</a> testing (medium).
|
||||
|
||||
@icode{cpp}
|
||||
std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array
|
||||
assert(backtracking::subset_sum::number_of_subsets(0, array1) ==
|
||||
2); // first argument in subset_sum function is the required sum and
|
||||
// second is the input array
|
||||
@endicode
|
||||
|
||||
3. Small C++ program that showcases and explains the use of tests.
|
||||
|
||||
@icode{cpp}
|
||||
#include <vector> /// for std::vector
|
||||
#include <cassert> /// for assert
|
||||
|
||||
/**
|
||||
* @brief Verifies if the given array
|
||||
* contains the given number on it.
|
||||
* @tparam T the type of array (e.g., `int`, `float`, etc.)
|
||||
* @param arr the array to be used for checking
|
||||
* @param number the number to check if it's inside the array
|
||||
* @return false if the number was NOT found in the array
|
||||
* @return true if the number WAS found in the array
|
||||
*/
|
||||
template <typename T>
|
||||
bool is_number_on_array(const std::vector<T> &arr, const int &number) {
|
||||
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
|
||||
if (arr[i] == number) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void tests() {
|
||||
std::vector<int> arr = { 9, 14, 21, 98, 67 };
|
||||
|
||||
assert(is_number_on_array(arr, 9) == true);
|
||||
assert(is_number_on_array(arr, 4) == false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
tests(); // run self-test implementations
|
||||
}
|
||||
@endicode
|
||||
|
||||
@subsubsection autotoc_md31 Typical structure of a program
|
||||
|
||||
@icode{cpp}
|
||||
/**
|
||||
* @file
|
||||
* @brief Add one line description here. Should contain a Wikipedia
|
||||
* @brief Add one-line description here. Should contain a Wikipedia
|
||||
* link or another source explaining the algorithm/implementation.
|
||||
* @details
|
||||
* This is a multi-line
|
||||
@@ -172,7 +244,7 @@ Contributor</h2>
|
||||
#include /// for `some function here`
|
||||
|
||||
/**
|
||||
* @namespace <folder name>
|
||||
* @namespace
|
||||
* @brief <namespace description>
|
||||
*/
|
||||
namespace name {
|
||||
@@ -232,7 +304,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
@endicode
|
||||
|
||||
@subsubsection autotoc_md31 File Name guidelines
|
||||
@subsubsection autotoc_md32 File Name guidelines
|
||||
|
||||
- Use lowercase words with <tt>"_"</tt> as a separator
|
||||
- For instance
|
||||
@@ -246,7 +318,7 @@ my_new_cpp_class.cpp is correct format
|
||||
- File name validation will run on Docker to ensure validity.
|
||||
- If an implementation of the algorithm already exists and your version is different from that implemented, please use incremental numeric digit as a suffix. For example: if <tt>median_search.cpp</tt> already exists in the <tt>search</tt> folder, and you are contributing a new implementation, the filename should be <tt>median_search2.cpp</tt>. For a third implementation, <tt>median_search3.cpp</tt>, and so on.
|
||||
|
||||
@subsubsection autotoc_md32 Directory guidelines
|
||||
@subsubsection autotoc_md33 Directory guidelines
|
||||
|
||||
- We recommend adding files to existing directories as much as possible.
|
||||
- Use lowercase words with <tt>"_"</tt> as separator ( no spaces or <tt>"-"</tt> allowed )
|
||||
@@ -260,7 +332,7 @@ some_new_fancy_category is correct
|
||||
- Filepaths will be used to dynamically create a directory of our algorithms.
|
||||
- Filepath validation will run on GitHub Actions to ensure compliance.
|
||||
|
||||
@paragraph autotoc_md33 Integrating CMake in a new directory
|
||||
@paragraph autotoc_md34 Integrating CMake in a new directory
|
||||
|
||||
In case a new directory is 100% required, <tt>CMakeLists.txt</tt> file in the root directory needs to be updated, and a new <tt>CMakeLists.txt</tt> file needs to be created within the new directory.
|
||||
|
||||
@@ -294,7 +366,7 @@ add_subdirectory(divide_and_conquer)
|
||||
add_subdirectory(<foldername>)
|
||||
@endicode
|
||||
|
||||
@subsubsection autotoc_md34 Commit Guidelines
|
||||
@subsubsection autotoc_md35 Commit Guidelines
|
||||
|
||||
- It is recommended to keep your changes grouped logically within individual commits. Maintainers find it easier to understand changes that are logically spilled across multiple commits. Try to modify just one or two files in the same directory. Pull requests that span multiple directories are often rejected.
|
||||
|
||||
@@ -321,11 +393,11 @@ Common prefixes:
|
||||
- test: Correct existing tests or add new ones
|
||||
- chore: Miscellaneous changes that do not match any of the above.
|
||||
|
||||
@subsection autotoc_md35 Pull Requests
|
||||
@subsection autotoc_md36 Pull Requests
|
||||
|
||||
- Checkout our <a href="<a href="https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.github/pull_request_template.md">https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.github/pull_request_template.md</a>" target="_blank" >pull request template</a>
|
||||
|
||||
@subsubsection autotoc_md36 Building Locally
|
||||
@subsubsection autotoc_md37 Building Locally
|
||||
|
||||
Before submitting a pull request,
|
||||
build the code locally or using the convenient <a href="<a href="https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus">https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus</a>" target="_blank" ><img src="<a href="https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod">https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod</a>" alt="Gitpod Ready-to-Code"/></a> service.
|
||||
@@ -334,7 +406,7 @@ Before submitting a pull request,
|
||||
cmake -B build -S .
|
||||
@endicode
|
||||
|
||||
@subsubsection autotoc_md37 Static Code Analyzer
|
||||
@subsubsection autotoc_md38 Static Code Analyzer
|
||||
|
||||
We use <a href="<a href="https://clang.llvm.org/extra/clang-tidy/">https://clang.llvm.org/extra/clang-tidy/</a>" target="_blank" ><tt>clang-tidy</tt></a> as a static code analyzer with a configuration in <a href="<a href="https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.clang-tidy">https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/.clang-tidy</a>" target="_blank" ><tt>.clang-tidy</tt></a>.
|
||||
|
||||
@@ -342,7 +414,7 @@ We use <a href="<a href="https://clang.llvm.org/extra/clang-tidy/">https://cl
|
||||
clang-tidy --fix --quiet -p build subfolder/file_to_check.cpp --
|
||||
@endicode
|
||||
|
||||
@subsubsection autotoc_md38 Code Formatter
|
||||
@subsubsection autotoc_md39 Code Formatter
|
||||
|
||||
<a href="<a href="https://clang.llvm.org/docs/ClangFormat.html">https://clang.llvm.org/docs/ClangFormat.html</a>" target="_blank" ><tt>clang-format</tt></a> is used for code formatting.
|
||||
|
||||
@@ -353,7 +425,7 @@ clang-tidy --fix --quiet -p build subfolder/file_to_check.cpp --
|
||||
- Linux (Debian): <tt>sudo apt-get install clang-format-10 clang-tidy-10</tt>
|
||||
- Running (all platforms): <tt>clang-format -i -style="file" my_file.cpp</tt>
|
||||
|
||||
@subsubsection autotoc_md39 GitHub Actions
|
||||
@subsubsection autotoc_md40 GitHub Actions
|
||||
|
||||
- Enable GitHub Actions on your fork of the repository.
|
||||
After enabling, it will execute <tt>clang-tidy</tt> and <tt>clang-format</tt> after every push (not a commit).
|
||||
|
||||
Reference in New Issue
Block a user