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(list_array): fix overflow check #1983

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Changes from 2 commits
Commits
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
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 <int N>
maximousblk marked this conversation as resolved.
Show resolved Hide resolved
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