Files
C-Plus-Plus/data_structures/list_array.cpp
realstealthninja c6af943508 fix: add cstdint header to all files using fixed width integers (#2717)
* fix: add <cstdint> to subset_sum.cpp

* fix: add <cstdint> to subarray_sum.cpp

* fix: add <cstdint> to wildcard_matching.cpp

* fix: add <cstdint> to count_bit_flips.cpp

* fix: add <cstdint> to count_of_set_bits.cpp

* fix: add <cstdint> to trailing_ciphers.cpp

* fix: add <cstdint> to hamming_distance.cpp

* doc: include doc for hamming_distance

* fix: add <cstdint> to next_higher_numebr_with_same_number_of_set_bits.cpp

* fix: add <cstdint> to power_of_2.cpp

* fix: add <cstdint> to set_kth_bit.cpp

* fix: add <cstdint> to bit_manipulation/set_kth_bit.cpp

* fix: add <cstdint> to bit_manipulation/travelling_salesman_using_bit_manipulation.cpp

* fix: add <cstdint> to ciphers/base64_encoding.cpp

* fix: add <cstdint> to ciphers/hill_cipher.cpp

* fix: add <cstdint> to ciphers/uint128_t.hpp

* fix: add <cstdint> to data_structures/dsu_path_compression.cpp

* fix: add <cstdint> to data_structures/dsu_path_compression.cpp

* fix add <cstdint> to datastructures/list_array>cpp

* fix add <cstdint> to datastructures/queue_using_array.cpp

* fix: add <cstdint> to sparse_table.cpp

* fix: add <cstdint> to stack_using_list_queue.cpp

* fix: add <cstdint> to treap.cpp

* fix: add <cstdint> to graham_scan_functions.hpp

* fix: add <cstdint> to graph/**

* fix: add integral typdefs to hashing/**

* fix: add <cstdint> to math/**

* fix: add <cstdint> to numerical_methods/**

* fix: add <cstdint> to other/**

* fix: add <cstdint> to search/**

* fix: add <cstdint> to sorting/**

* fix: add <cstdint> to string/**

* doc: remove include statement from comment

* fix: make tests static

Co-authored-by: David Leal <halfpacho@gmail.com>

* fix: make tests static

Co-authored-by: David Leal <halfpacho@gmail.com>

* chore: use iwyu on backtracking/**.cpp

* chore: use iwyu on bit_manip/**.cpp

* chore: use iwyu on ciphers/**.cpp

* chore: use iwyu on cpu_scheduling_algorithms/**.cpp

* chore: use iwyu on data_structures/**.cpp

* chore: use iwyu on divide_and_conquer/**.cpp

* chore: use iwyu on geometry/**.cpp

* chore: use iwyu on graph/**.cpp

* chore: use iwyu on hashing/**.cpp

* chore: use iwyu on machine_learning/**.cpp

* chore: use iwyu on math/**.cpp

* chore: use iwyu on numerical_methods/**.cpp

* chore: use iwyu on others/**.cpp

* chore: use iwyu on probablity/**.cpp

* chore: use iwyu on search/**.cpp

* chore: use iwyu on sorting/**.cpp

* chore: use iwyu on strings/**.cpp

* Revert "chore: use iwyu on strings/**.cpp"

This reverts commit f2127456a8.

* Revert "chore: use iwyu on sorting/**.cpp"

This reverts commit a290ae7ee2.

* Revert "chore: use iwyu on search/**.cpp"

This reverts commit 19d136ae0f.

* Revert "chore: use iwyu on probablity/**.cpp"

This reverts commit 5dd7f82a34.

* Revert "chore: use iwyu on others/**.cpp"

This reverts commit 8a8fd42383.

* Revert "chore: use iwyu on numerical_methods/**.cpp"

This reverts commit eff2f44a50.

* Revert "chore: use iwyu on math/**.cpp"

This reverts commit c47117ca3f.

* Revert "chore: use iwyu on machine_learning/**.cpp"

This reverts commit c3897d3763.

* Revert "chore: use iwyu on hashing/**.cpp"

This reverts commit 0c6611a835.

* Revert "chore: use iwyu on graph/**.cpp"

This reverts commit dabd6d2591.

* Revert "chore: use iwyu on geometry/**.cpp"

This reverts commit 740bd65932.

* Revert "chore: use iwyu on divide_and_conquer/**.cpp"

This reverts commit 16ee49e086.

* Revert "chore: use iwyu on data_structures/**.cpp"

This reverts commit a3b719e368.

* Revert "chore: use iwyu on cpu_scheduling_algorithms/**.cpp"

This reverts commit 24e597f7e2.

* Revert "chore: use iwyu on ciphers/**.cpp"

This reverts commit 3d80295883.

* Revert "chore: use iwyu on bit_manip/**.cpp"

This reverts commit 7edcb6e458.

* Revert "chore: use iwyu on backtracking/**.cpp"

This reverts commit f0a30d7cdb.

* Update search/binary_search.cpp

* Update backtracking/subarray_sum.cpp

* Update backtracking/subset_sum.cpp

* Update backtracking/wildcard_matching.cpp

* Update bit_manipulation/count_bits_flip.cpp

* Update bit_manipulation/count_of_set_bits.cpp

* Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp

* Update bit_manipulation/hamming_distance.cpp

* Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp

* Update bit_manipulation/power_of_2.cpp

* Update others/lru_cache.cpp

* Update bit_manipulation/set_kth_bit.cpp

* Update bit_manipulation/travelling_salesman_using_bit_manipulation.cpp

* Update ciphers/base64_encoding.cpp

* Update ciphers/hill_cipher.cpp

* Update ciphers/uint128_t.hpp

* Update cpu_scheduling_algorithms/fcfs_scheduling.cpp

* Update data_structures/dsu_path_compression.cpp

* Update data_structures/dsu_union_rank.cpp

* Update data_structures/list_array.cpp

* Update data_structures/queue_using_array.cpp

* Update data_structures/sparse_table.cpp

* Update data_structures/stack_using_queue.cpp

* Update data_structures/treap.cpp

* Update geometry/graham_scan_functions.hpp

* Update graph/bidirectional_dijkstra.cpp

* Update graph/connected_components_with_dsu.cpp

* Update graph/cycle_check_directed_graph.cpp

* Update graph/is_graph_bipartite2.cpp

* Update graph/travelling_salesman_problem.cpp

* Update hashing/md5.cpp

* Update hashing/sha1.cpp

* Update math/n_choose_r.cpp

* Update strings/z_function.cpp

* Update strings/manacher_algorithm.cpp

* Update sorting/wiggle_sort.cpp

* Update sorting/selection_sort_recursive.cpp

* Update sorting/selection_sort_iterative.cpp

* Update sorting/recursive_bubble_sort.cpp

* Update sorting/radix_sort2.cpp

* Update sorting/dnf_sort.cpp

* Update sorting/cycle_sort.cpp

* Update search/sublist_search.cpp

* Update search/saddleback_search.cpp

* Update search/interpolation_search.cpp

* Update search/floyd_cycle_detection_algo.cpp

* Update search/exponential_search.cpp

* Update search/exponential_search.cpp

* Update math/n_bonacci.cpp

* Update math/aliquot_sum.cpp

* Update math/check_factorial.cpp

* Update math/double_factorial.cpp

* Update math/eulers_totient_function.cpp

* Update math/factorial.cpp

* Update math/fibonacci.cpp

* Update math/fibonacci_matrix_exponentiation.cpp

* Update math/fibonacci_sum.cpp

* Update math/finding_number_of_digits_in_a_number.cpp

* chore: remove "/// for integral typedefs"

* chore: remove for integral typedefs from modular division

* fix: remove comment from include

* fix: add cstdint to gale shapely

---------

Co-authored-by: David Leal <halfpacho@gmail.com>
2024-11-04 17:38:54 +05:30

264 lines
8.5 KiB
C++

/**
* @file
* @brief [Dynamic Array](https://en.wikipedia.org/wiki/Dynamic_array)
*
* @details
* The list_array is the implementation of list represented using array.
* We can perform basic CRUD operations as well as other operations like sorting
* etc.
*
* ### Algorithm
* It implements various method like insert, sort, search etc. efficiently.
* You can select the operation and methods will do the rest work for you.
* You can insert element, sort them in order, search efficiently, delete values
* and print the list.
*/
#include <array> /// for std::array
#include <cassert> /// for assert
#include <cstdint>
#include <iostream> /// for io operations
/**
* @namespace data_structures
* @brief Algorithms with data structures
*/
namespace data_structures {
/**
* @namespace list_array
* @brief Functions for [Dynamic
* Array](https://en.wikipedia.org/wiki/Dynamic_array) algorithm
*/
namespace list_array {
/**
* @brief Structure of List with supporting methods.
*/
template <uint64_t N>
struct list {
std::array<uint64_t, N> data{}; // Array that implement list
uint64_t top = 0; // Pointer to the last element
bool isSorted = false; // indicator whether list is sorted or not
/**
* @brief Search an element in the list using binarySearch.
* @param dataArr list
* @param first pointer to the first element in the remaining list
* @param last pointer to the last element in the remaining list
* @param val element that will be searched
* @return index of element in the list if present else -1
*/
uint64_t BinarySearch(const std::array<uint64_t, N> &dataArr,
const uint64_t &first, const uint64_t &last,
const uint64_t &val) {
// If both pointer cross each other means no element present in the list
// which is equal to the val
if (last < first) {
return -1;
}
uint64_t mid = (first + last) / 2;
// check whether current mid pointer value is equal to element or not
if (dataArr[mid] == val)
return mid;
// if current mid value is greater than element we have to search in
// first half
else if (val < dataArr[mid])
return (BinarySearch(dataArr, first, mid - 1, val));
// if current mid value is greater than element we have to search in
// second half
else if (val > dataArr[mid])
return (BinarySearch(dataArr, mid + 1, last, val));
std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n";
return -1;
}
/**
* @brief Search an element using linear search
* @param dataArr list
* @param val element that will be searched
* @return index of element in the list if present else -1
*/
uint64_t LinearSearch(const std::array<uint64_t, N> &dataArr,
const uint64_t &val) const {
// Going through each element in the list
for (uint64_t i = 0; i < top; i++) {
if (dataArr[i] == val) {
return i; // element found at ith index
}
}
// element is not present in the list
return -1;
}
/*
* @brief Parent function of binarySearch and linearSearch methods
* @param val element that will be searched
* @return index of element in the list if present else -1
*/
uint64_t search(const uint64_t &val) {
uint64_t pos; // pos variable to store index value of element.
// if list is sorted, binary search works efficiently else linear search
// is the only option
if (isSorted) {
pos = BinarySearch(data, 0, top - 1, val);
} else {
pos = LinearSearch(data, val);
}
// if index is equal to -1 means element does not present
// else print the index of that element
if (pos != -1) {
std::cout << "\nElement found at position : " << pos;
} else {
std::cout << "\nElement not found";
}
// return the index of element or -1.
return pos;
}
/**
* @brief Sort the list
* @returns void
*/
void sort() {
// Going through each element in the list
for (uint64_t i = 0; i < top; i++) {
uint64_t min_idx = i; // Initialize the min variable
for (uint64_t j = i + 1; j < top; j++) {
// check whether any element less than current min value
if (data[j] < data[min_idx]) {
min_idx = j; // update index accordingly
}
}
// swap min value and element at the ith index
std::swap(data[min_idx], data[i]);
}
// mark isSorted variable as true
isSorted = true;
}
/**
* @brief Insert the new element in the list
* @param val element that will be inserted
* @returns void
*/
void insert(const uint64_t &val) {
// overflow check
if (top == N) {
std::cout << "\nOverflow";
return;
}
// if list is not sorted, insert at the last
// otherwise place it to correct position
if (!isSorted) {
data[top] = val;
top++;
} else {
uint64_t pos = 0; // Initialize the index variable
// Going through each element and find correct position for element
for (uint64_t i = 0; i < top - 1; i++) {
// check for the correct position
if (data[i] <= val && val <= data[i + 1]) {
pos = i + 1; // assign correct pos to the index var
break; // to get out from the loop
}
}
// if all elements are smaller than the element
if (pos == 0) {
pos = top - 1;
}
// shift all element to make a room for new element
for (uint64_t i = top; i > pos; i--) {
data[i] = data[i - 1];
}
top++; // Increment the value of top.
data[pos] =
val; // Assign the value to the correct index in the array
}
}
/**
* @brief To remove the element from the list
* @param val element that will be removed
* @returns void
*/
void remove(const uint64_t &val) {
uint64_t pos = search(val); // search the index of the value
// if search returns -1, element does not present in the list
if (pos == -1) {
std::cout << "\n Element does not present in the list ";
return;
}
std::cout << "\n"
<< data[pos] << " deleted"; // print the appropriate message
// shift all the element 1 left to fill vacant space
for (uint64_t i = pos; i < top; i++) {
data[i] = data[i + 1];
}
top--; // decrement the top variable to maintain last index
}
/**
* @brief Utility function to print array
* @returns void
*/
void show() {
// Going through each element in the list
std::cout << '\n';
for (uint64_t i = 0; i < top; i++) {
std::cout << data[i] << " "; // print the element
}
}
}; // structure list
} // namespace list_array
} // namespace data_structures
/**
* @brief Test implementations
* @returns void
*/
static void test() {
data_structures::list_array::list<50> L;
// Insert testing
L.insert(11);
L.insert(12);
assert(L.top == 2);
L.insert(15);
L.insert(10);
L.insert(12);
L.insert(20);
L.insert(18);
assert(L.top == 7);
L.show(); // To print the array
// Remove testing
L.remove(12); // Remove Duplicate value in the list
L.remove(15); // Remove the existing value in the list
assert(L.top == 5);
L.remove(50); // Try to remove the non-existing value in the list
assert(L.top == 5);
// LinearSearch testing
assert(L.search(11) == 0); // search for the existing element
assert(L.search(12) == 2);
assert(L.search(50) == -1); // search for the non-existing element
// Sort testing
L.sort();
assert(L.isSorted == true);
L.show();
// BinarySearch testing
assert(L.search(11) == 1); // search for the existing element
assert(L.search(12) == 2);
assert(L.search(50) == -1); // search for the non-existing element
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // Execute the tests
return 0;
}