From 8a24e44a4c4fddd0a3ce9f5b12ed1f9e66657a6c Mon Sep 17 00:00:00 2001 From: Sagar Pandya <31953933+sagarpandyansit@users.noreply.github.com> Date: Thu, 22 Oct 2020 23:10:22 +0530 Subject: [PATCH] fix: Issue in list_array.cpp resolved (#1342) * Exit code added to exit from infinite loop * fix typos and resolve all clang-tidy warnings * changed C type array style and changed code accordingly in list_array.cpp * pass const array to resolve clang-tidy error in list_array.cpp * Update newline in list_array.cpp Co-authored-by: David Leal Co-authored-by: David Leal --- data_structures/list_array.cpp | 73 +++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/data_structures/list_array.cpp b/data_structures/list_array.cpp index 24b04867b..6cbf8e154 100644 --- a/data_structures/list_array.cpp +++ b/data_structures/list_array.cpp @@ -4,31 +4,32 @@ * @warning The sorting algorithm is erroneous */ #include +#include struct list { - int data[50]; + std::array data{}; int top = 0; bool isSorted = false; - int BinarySearch(int *array, int first, int last, int x) { + int BinarySearch(const std::array& dataArr, int first, int last, int x) { if (last < first) { return -1; } int mid = (first + last) / 2; - if (array[mid] == x) + if (dataArr[mid] == x) return mid; - else if (x < array[mid]) - return (BinarySearch(array, first, mid - 1, x)); - else if (x > array[mid]) - return (BinarySearch(array, mid + 1, last, x)); + else if (x < dataArr[mid]) + return (BinarySearch(dataArr, first, mid - 1, x)); + else if (x > dataArr[mid]) + return (BinarySearch(dataArr, mid + 1, last, x)); std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n"; return -1; } - int LinarSearch(int *array, int x) { + int LinearSearch(const std::array& dataArr, int x) const { for (int i = 0; i < top; i++) { - if (array[i] == x) { + if (dataArr[i] == x) { return i; } } @@ -37,12 +38,12 @@ struct list { } int Search(int x) { - int pos = -1; + int pos; if (isSorted) { pos = BinarySearch(data, 0, top - 1, x); } else { - pos = LinarSearch(data, x); + pos = LinearSearch(data, x); } if (pos != -1) { @@ -54,7 +55,7 @@ struct list { } void Sort() { - int i, j, pos; + int i, j, pos=0; for (i = 0; i < top; i++) { int min = data[i]; for (j = i + 1; j < top; j++) { @@ -121,6 +122,8 @@ int main() { int choice; int x; do { + // Choices for operations on the list_array. + std::cout << "\n0.Exit"; std::cout << "\n1.Insert"; std::cout << "\n2.Delete"; std::cout << "\n3.Search"; @@ -129,27 +132,31 @@ int main() { std::cout << "\n\nEnter Your Choice : "; std::cin >> choice; switch (choice) { - case 1: - std::cout << "\nEnter the element to be inserted : "; - std::cin >> x; - L.insert(x); - break; - case 2: - std::cout << "\nEnter the element to be removed : "; - std::cin >> x; - L.Remove(x); - break; - case 3: - std::cout << "\nEnter the element to be searched : "; - std::cin >> x; - L.Search(x); - break; - case 4: - L.Sort(); - break; - case 5: - L.Show(); - break; + case 0: + break; + case 1: + std::cout << "\nEnter the element to be inserted : "; + std::cin >> x; + L.insert(x); + break; + case 2: + std::cout << "\nEnter the element to be removed : "; + std::cin >> x; + L.Remove(x); + break; + case 3: + std::cout << "\nEnter the element to be searched : "; + std::cin >> x; + L.Search(x); + break; + case 4: + L.Sort(); + break; + case 5: + L.Show(); + break; + default: + std::cout << "\nplease enter valid option."; } } while (choice != 0); return 0;