Skip to content

Commit

Permalink
fix: fix overflow check in data_structures/list_array.cpp (#1983)
Browse files Browse the repository at this point in the history
* fix(list_array): fix overflow check

+ use template for list size

* Update data_structures/list_array.cpp

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

Co-authored-by: David Leal <halfpacho@gmail.com>
  • Loading branch information
maximousblk and Panquesito7 authored Sep 2, 2022
1 parent 4b7982a commit db149bf
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions data_structures/list_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ namespace list_array {
/**
* @brief Structure of List with supporting methods.
*/
template <uint64_t N>
struct list {
std::array<uint64_t, 50> data{}; // Array that implement 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
/**
Expand All @@ -41,7 +42,7 @@ namespace list_array {
* @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, 50> &dataArr, const uint64_t &first, const uint64_t &last,
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) {
Expand All @@ -68,7 +69,7 @@ namespace list_array {
* @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, 50> &dataArr, const uint64_t &val) const {
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) {
Expand Down Expand Up @@ -131,7 +132,7 @@ namespace list_array {
*/
void insert(const uint64_t &val) {
// overflow check
if (top == 49) {
if (top == N) {
std::cout << "\nOverflow";
return;
}
Expand Down Expand Up @@ -203,7 +204,7 @@ namespace list_array {
* @returns void
*/
static void test() {
data_structures::list_array::list L;
data_structures::list_array::list<50> L;

// Insert testing
L.insert(11);
Expand Down

0 comments on commit db149bf

Please sign in to comment.