Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add cstdint header to all files using fixed width integers #2717

Merged
merged 145 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
9c138a5
fix: add <cstdint> to subset_sum.cpp
realstealthninja Aug 31, 2024
6629fd2
fix: add <cstdint> to subarray_sum.cpp
realstealthninja Aug 31, 2024
392aed2
fix: add <cstdint> to wildcard_matching.cpp
realstealthninja Aug 31, 2024
f71bd16
fix: add <cstdint> to count_bit_flips.cpp
realstealthninja Aug 31, 2024
79104ec
fix: add <cstdint> to count_of_set_bits.cpp
realstealthninja Aug 31, 2024
d550381
fix: add <cstdint> to trailing_ciphers.cpp
realstealthninja Aug 31, 2024
b66991f
fix: add <cstdint> to hamming_distance.cpp
realstealthninja Aug 31, 2024
13f09a6
doc: include doc for hamming_distance
realstealthninja Aug 31, 2024
6b986a9
fix: add <cstdint> to next_higher_numebr_with_same_number_of_set_bits…
realstealthninja Aug 31, 2024
9639d76
fix: add <cstdint> to power_of_2.cpp
realstealthninja Aug 31, 2024
8517a66
fix: add <cstdint> to set_kth_bit.cpp
realstealthninja Aug 31, 2024
49fbaf6
fix: add <cstdint> to bit_manipulation/set_kth_bit.cpp
realstealthninja Aug 31, 2024
4b16caa
fix: add <cstdint> to bit_manipulation/travelling_salesman_using_bit_…
realstealthninja Aug 31, 2024
84c2957
fix: add <cstdint> to ciphers/base64_encoding.cpp
realstealthninja Aug 31, 2024
89dfe60
fix: add <cstdint> to ciphers/hill_cipher.cpp
realstealthninja Aug 31, 2024
1a0b5cf
fix: add <cstdint> to ciphers/uint128_t.hpp
realstealthninja Aug 31, 2024
4792c81
fix: add <cstdint> to data_structures/dsu_path_compression.cpp
realstealthninja Aug 31, 2024
11d2437
fix: add <cstdint> to data_structures/dsu_path_compression.cpp
realstealthninja Aug 31, 2024
ef59b45
fix add <cstdint> to datastructures/list_array>cpp
realstealthninja Aug 31, 2024
b8e5592
fix add <cstdint> to datastructures/queue_using_array.cpp
realstealthninja Aug 31, 2024
0d18442
fix: add <cstdint> to sparse_table.cpp
realstealthninja Aug 31, 2024
82cb276
fix: add <cstdint> to stack_using_list_queue.cpp
realstealthninja Aug 31, 2024
5421cfc
fix: add <cstdint> to treap.cpp
realstealthninja Aug 31, 2024
b752f55
fix: add <cstdint> to graham_scan_functions.hpp
realstealthninja Aug 31, 2024
549aad5
fix: add <cstdint> to graph/**
realstealthninja Aug 31, 2024
2ec9d7f
fix: add integral typdefs to hashing/**
realstealthninja Aug 31, 2024
afd4ccb
fix: add <cstdint> to math/**
realstealthninja Aug 31, 2024
8740df0
fix: add <cstdint> to numerical_methods/**
realstealthninja Aug 31, 2024
6357548
fix: add <cstdint> to other/**
realstealthninja Aug 31, 2024
52ff5e8
fix: add <cstdint> to search/**
realstealthninja Aug 31, 2024
4707b72
fix: add <cstdint> to sorting/**
realstealthninja Aug 31, 2024
ba64b3c
fix: add <cstdint> to string/**
realstealthninja Aug 31, 2024
52b2afe
doc: remove include statement from comment
realstealthninja Aug 31, 2024
3aa689c
Merge branch 'master' into cstdint
realstealthninja Aug 31, 2024
e6bafa6
Merge branch 'master' into cstdint
realstealthninja Sep 2, 2024
56d17d9
Merge branch 'master' into cstdint
realstealthninja Sep 2, 2024
a147054
Merge branch 'master' into cstdint
realstealthninja Sep 3, 2024
d1b4566
Merge branch 'master' into cstdint
Panquesito7 Sep 4, 2024
fad74e8
fix: make tests static
realstealthninja Sep 4, 2024
f063687
fix: make tests static
realstealthninja Sep 4, 2024
11c5fde
Merge branch 'master' into cstdint
realstealthninja Sep 4, 2024
55f3d1d
Merge branch 'master' into cstdint
realstealthninja Sep 5, 2024
f0a30d7
chore: use iwyu on backtracking/**.cpp
realstealthninja Sep 13, 2024
7edcb6e
chore: use iwyu on bit_manip/**.cpp
realstealthninja Sep 13, 2024
3d80295
chore: use iwyu on ciphers/**.cpp
realstealthninja Sep 13, 2024
24e597f
chore: use iwyu on cpu_scheduling_algorithms/**.cpp
realstealthninja Sep 13, 2024
a3b719e
chore: use iwyu on data_structures/**.cpp
realstealthninja Sep 13, 2024
16ee49e
chore: use iwyu on divide_and_conquer/**.cpp
realstealthninja Sep 13, 2024
740bd65
chore: use iwyu on geometry/**.cpp
realstealthninja Sep 13, 2024
dabd6d2
chore: use iwyu on graph/**.cpp
realstealthninja Sep 13, 2024
0c6611a
chore: use iwyu on hashing/**.cpp
realstealthninja Sep 13, 2024
c3897d3
chore: use iwyu on machine_learning/**.cpp
realstealthninja Sep 13, 2024
c47117c
chore: use iwyu on math/**.cpp
realstealthninja Sep 13, 2024
eff2f44
chore: use iwyu on numerical_methods/**.cpp
realstealthninja Sep 13, 2024
8a8fd42
chore: use iwyu on others/**.cpp
realstealthninja Sep 13, 2024
5dd7f82
chore: use iwyu on probablity/**.cpp
realstealthninja Sep 13, 2024
19d136a
chore: use iwyu on search/**.cpp
realstealthninja Sep 13, 2024
a290ae7
chore: use iwyu on sorting/**.cpp
realstealthninja Sep 13, 2024
f212745
chore: use iwyu on strings/**.cpp
realstealthninja Sep 13, 2024
00c5e91
Revert "chore: use iwyu on strings/**.cpp"
realstealthninja Sep 13, 2024
cb6eaed
Revert "chore: use iwyu on sorting/**.cpp"
realstealthninja Sep 13, 2024
8e99858
Revert "chore: use iwyu on search/**.cpp"
realstealthninja Sep 13, 2024
958088f
Revert "chore: use iwyu on probablity/**.cpp"
realstealthninja Sep 13, 2024
1694bde
Revert "chore: use iwyu on others/**.cpp"
realstealthninja Sep 13, 2024
5b65101
Revert "chore: use iwyu on numerical_methods/**.cpp"
realstealthninja Sep 13, 2024
d9e6762
Revert "chore: use iwyu on math/**.cpp"
realstealthninja Sep 13, 2024
3b86feb
Revert "chore: use iwyu on machine_learning/**.cpp"
realstealthninja Sep 13, 2024
fe87144
Revert "chore: use iwyu on hashing/**.cpp"
realstealthninja Sep 13, 2024
0ed3409
Revert "chore: use iwyu on graph/**.cpp"
realstealthninja Sep 13, 2024
5300883
Revert "chore: use iwyu on geometry/**.cpp"
realstealthninja Sep 13, 2024
380127e
Revert "chore: use iwyu on divide_and_conquer/**.cpp"
realstealthninja Sep 13, 2024
1901732
Revert "chore: use iwyu on data_structures/**.cpp"
realstealthninja Sep 13, 2024
d0af25f
Revert "chore: use iwyu on cpu_scheduling_algorithms/**.cpp"
realstealthninja Sep 13, 2024
d8895b7
Revert "chore: use iwyu on ciphers/**.cpp"
realstealthninja Sep 13, 2024
7920c50
Revert "chore: use iwyu on bit_manip/**.cpp"
realstealthninja Sep 13, 2024
bcae88d
Revert "chore: use iwyu on backtracking/**.cpp"
realstealthninja Sep 13, 2024
8d7f865
Merge branch 'master' into cstdint
realstealthninja Sep 13, 2024
f60bc8b
Update search/binary_search.cpp
realstealthninja Sep 30, 2024
523d02e
Update backtracking/subarray_sum.cpp
realstealthninja Sep 30, 2024
9c35a06
Update backtracking/subset_sum.cpp
realstealthninja Sep 30, 2024
4e49a13
Update backtracking/wildcard_matching.cpp
realstealthninja Sep 30, 2024
9a1c789
Update bit_manipulation/count_bits_flip.cpp
realstealthninja Sep 30, 2024
8a7eec5
Update bit_manipulation/count_of_set_bits.cpp
realstealthninja Sep 30, 2024
503f3ae
Update bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp
realstealthninja Sep 30, 2024
c6e8264
Update bit_manipulation/hamming_distance.cpp
realstealthninja Sep 30, 2024
b31776d
Update bit_manipulation/next_higher_number_with_same_number_of_set_bi…
realstealthninja Sep 30, 2024
20073f3
Update bit_manipulation/power_of_2.cpp
realstealthninja Sep 30, 2024
f509005
Update others/lru_cache.cpp
realstealthninja Sep 30, 2024
12f0d52
Update bit_manipulation/set_kth_bit.cpp
realstealthninja Sep 30, 2024
49f0be3
Update bit_manipulation/travelling_salesman_using_bit_manipulation.cpp
realstealthninja Sep 30, 2024
deaecb9
Update ciphers/base64_encoding.cpp
realstealthninja Sep 30, 2024
f94271c
Update ciphers/hill_cipher.cpp
realstealthninja Sep 30, 2024
054e37c
Update ciphers/uint128_t.hpp
realstealthninja Sep 30, 2024
b34dbff
Update cpu_scheduling_algorithms/fcfs_scheduling.cpp
realstealthninja Sep 30, 2024
b20f01c
Update data_structures/dsu_path_compression.cpp
realstealthninja Sep 30, 2024
465eb7b
Update data_structures/dsu_union_rank.cpp
realstealthninja Sep 30, 2024
04c9f15
Update data_structures/list_array.cpp
realstealthninja Sep 30, 2024
bbbd9ab
Update data_structures/queue_using_array.cpp
realstealthninja Sep 30, 2024
227e9d2
Update data_structures/sparse_table.cpp
realstealthninja Sep 30, 2024
572bb35
Update data_structures/stack_using_queue.cpp
realstealthninja Sep 30, 2024
5e53d92
Update data_structures/treap.cpp
realstealthninja Sep 30, 2024
99839c3
Update geometry/graham_scan_functions.hpp
realstealthninja Sep 30, 2024
21446ef
Update graph/bidirectional_dijkstra.cpp
realstealthninja Sep 30, 2024
7ccca60
Update graph/connected_components_with_dsu.cpp
realstealthninja Sep 30, 2024
d6ea502
Update graph/cycle_check_directed_graph.cpp
realstealthninja Sep 30, 2024
016a2d8
Update graph/is_graph_bipartite2.cpp
realstealthninja Sep 30, 2024
929af50
Update graph/travelling_salesman_problem.cpp
realstealthninja Sep 30, 2024
5d6fa2b
Update hashing/md5.cpp
realstealthninja Sep 30, 2024
ac91c01
Update hashing/sha1.cpp
realstealthninja Sep 30, 2024
1abafa4
Update math/n_choose_r.cpp
realstealthninja Sep 30, 2024
10373fe
Update strings/z_function.cpp
realstealthninja Sep 30, 2024
ca6577d
Update strings/manacher_algorithm.cpp
realstealthninja Sep 30, 2024
6c2158e
Update sorting/wiggle_sort.cpp
realstealthninja Sep 30, 2024
4b6c5ff
Update sorting/selection_sort_recursive.cpp
realstealthninja Sep 30, 2024
15f8b88
Update sorting/selection_sort_iterative.cpp
realstealthninja Sep 30, 2024
70393b7
Update sorting/recursive_bubble_sort.cpp
realstealthninja Sep 30, 2024
f526abe
Update sorting/radix_sort2.cpp
realstealthninja Sep 30, 2024
929f1ee
Update sorting/dnf_sort.cpp
realstealthninja Sep 30, 2024
768534b
Update sorting/cycle_sort.cpp
realstealthninja Sep 30, 2024
01e2635
Update search/sublist_search.cpp
realstealthninja Sep 30, 2024
c1949c1
Update search/saddleback_search.cpp
realstealthninja Sep 30, 2024
eb9043c
Update search/interpolation_search.cpp
realstealthninja Sep 30, 2024
e01b493
Update search/floyd_cycle_detection_algo.cpp
realstealthninja Sep 30, 2024
f042a15
Update search/exponential_search.cpp
realstealthninja Sep 30, 2024
10e2edb
Update search/exponential_search.cpp
realstealthninja Sep 30, 2024
cd84f82
Merge branch 'master' into cstdint
realstealthninja Sep 30, 2024
229bb39
Update math/n_bonacci.cpp
realstealthninja Sep 30, 2024
35b6bfc
Update math/aliquot_sum.cpp
realstealthninja Sep 30, 2024
78cb6ea
Update math/check_factorial.cpp
realstealthninja Sep 30, 2024
cb98ef5
Update math/double_factorial.cpp
realstealthninja Sep 30, 2024
cb6ac14
Update math/eulers_totient_function.cpp
realstealthninja Sep 30, 2024
16d4ed9
Update math/factorial.cpp
realstealthninja Sep 30, 2024
af35499
Update math/fibonacci.cpp
realstealthninja Sep 30, 2024
0ddbef6
Update math/fibonacci_matrix_exponentiation.cpp
realstealthninja Sep 30, 2024
57722cc
Update math/fibonacci_sum.cpp
realstealthninja Sep 30, 2024
a7c8741
Update math/finding_number_of_digits_in_a_number.cpp
realstealthninja Sep 30, 2024
db9ebd9
chore: remove "/// for integral typedefs"
realstealthninja Sep 30, 2024
2d7f443
chore: remove for integral typedefs from modular division
realstealthninja Sep 30, 2024
2f03640
Merge branch 'master' into cstdint
realstealthninja Oct 4, 2024
39cd383
fix: remove comment from include
realstealthninja Oct 4, 2024
accd220
Merge branch 'TheAlgorithms:master' into cstdint
realstealthninja Oct 4, 2024
030431f
fix: add cstdint to gale shapely
realstealthninja Oct 4, 2024
d40099a
Merge branch 'master' into cstdint
realstealthninja Oct 6, 2024
4e4d931
Merge branch 'master' into cstdint
realstealthninja Oct 10, 2024
89001cc
Merge branch 'master' into cstdint
realstealthninja Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix add <cstdint> to datastructures/list_array>cpp
realstealthninja committed Aug 31, 2024

Verified

This commit was signed with the committer’s verified signature.
commit ef59b45129cb4f8f3df5af30fa4e5b27e4282a99
344 changes: 178 additions & 166 deletions data_structures/list_array.cpp
Original file line number Diff line number Diff line change
@@ -4,17 +4,20 @@
*
* @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.
* 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.
* You can insert element, sort them in order, search efficiently, delete values
* and print the list.
*/

#include <iostream> /// for io operations
#include <array> /// for std::array
#include <cassert> /// for assert
#include <array> /// for std::array
#include <cassert> /// for assert
#include <cstdint> /// for integral typedefs
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
#include <iostream> /// for io operations

/**
* @namespace data_structures
@@ -23,181 +26,190 @@
namespace data_structures {
/**
* @namespace list_array
* @brief Functions for [Dynamic Array](https://en.wikipedia.org/wiki/Dynamic_array) algorithm
* @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 Structure of List with supporting methods.
* @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
*/
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";
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));

/**
* @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
}
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;
}
// 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 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
}
/**
* @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;
// 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];
/**
* @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
}
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;
// if all elements are smaller than the element
if (pos == 0) {
pos = top - 1;
}
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];
// shift all element to make a room for new element
for (uint64_t i = top; i > pos; i--) {
data[i] = data[i - 1];
}
top--; // decrement the top variable to maintain last index
top++; // Increment the value of top.
data[pos] =
val; // Assign the value to the correct index in the array
}
}

/**
* @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
}
/**
* @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
}
}; // structure list
} // namespace list_array
} // namespace data_structures

/**
* @brief Test implementations
@@ -216,36 +228,36 @@ static void test() {
L.insert(20);
L.insert(18);
assert(L.top == 7);
L.show(); // To print the array
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
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
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(11) == 0); // search for the existing element
assert(L.search(12) == 2);
assert(L.search(50) == -1); // search for the non-existing element
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(11) == 1); // search for the existing element
assert(L.search(12) == 2);
assert(L.search(50) == -1); // search for the non-existing element
assert(L.search(50) == -1); // search for the non-existing element
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // Execute the tests
test(); // Execute the tests
return 0;
}