Merge pull request #885 from kvedala/lgtm_fix

[bug fix] errors reported by lgtm for #880
This commit is contained in:
Krishna Vedala 2020-06-23 12:10:10 -04:00 committed by GitHub
commit 445f6722d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 25 deletions

View File

@ -1,5 +1,9 @@
/**
* @file list_array.cpp
* @todo Add documentation
* @warning The sorting algorithm is erroneous
*/
#include <iostream> #include <iostream>
using namespace std;
struct list { struct list {
int data[50]; int data[50];
@ -17,6 +21,9 @@ struct list {
return (BinarySearch(array, first, mid - 1, x)); return (BinarySearch(array, first, mid - 1, x));
else if (x > array[mid]) else if (x > array[mid])
return (BinarySearch(array, mid + 1, last, x)); return (BinarySearch(array, mid + 1, last, x));
std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n";
return -1;
} }
int LinarSearch(int *array, int x) { int LinarSearch(int *array, int x) {
@ -34,16 +41,14 @@ struct list {
if (isSorted) { if (isSorted) {
pos = BinarySearch(data, 0, top - 1, x); pos = BinarySearch(data, 0, top - 1, x);
} } else {
else {
pos = LinarSearch(data, x); pos = LinarSearch(data, x);
} }
if (pos != -1) { if (pos != -1) {
cout << "\nElement found at position : " << pos; std::cout << "\nElement found at position : " << pos;
} else { } else {
cout << "\nElement not found"; std::cout << "\nElement not found";
} }
return pos; return pos;
} }
@ -69,14 +74,12 @@ struct list {
void insert(int x) { void insert(int x) {
if (!isSorted) { if (!isSorted) {
if (top == 49) { if (top == 49) {
cout << "\nOverflow"; std::cout << "\nOverflow";
} else { } else {
data[top] = x; data[top] = x;
top++; top++;
} }
} } else {
else {
int pos = 0; int pos = 0;
for (int i = 0; i < top - 1; i++) { for (int i = 0; i < top - 1; i++) {
@ -99,7 +102,7 @@ struct list {
void Remove(int x) { void Remove(int x) {
int pos = Search(x); int pos = Search(x);
cout << "\n" << data[pos] << " deleted"; std::cout << "\n" << data[pos] << " deleted";
for (int i = pos; i < top; i++) { for (int i = pos; i < top; i++) {
data[i] = data[i + 1]; data[i] = data[i + 1];
} }
@ -108,7 +111,7 @@ struct list {
void Show() { void Show() {
for (int i = 0; i < top; i++) { for (int i = 0; i < top; i++) {
cout << data[i] << "\t"; std::cout << data[i] << "\t";
} }
} }
}; };
@ -118,27 +121,27 @@ int main() {
int choice; int choice;
int x; int x;
do { do {
cout << "\n1.Insert"; std::cout << "\n1.Insert";
cout << "\n2.Delete"; std::cout << "\n2.Delete";
cout << "\n3.Search"; std::cout << "\n3.Search";
cout << "\n4.Sort"; std::cout << "\n4.Sort";
cout << "\n5.Print"; std::cout << "\n5.Print";
cout << "\n\nEnter Your Choice : "; std::cout << "\n\nEnter Your Choice : ";
cin >> choice; std::cin >> choice;
switch (choice) { switch (choice) {
case 1: case 1:
cout << "\nEnter the element to be inserted : "; std::cout << "\nEnter the element to be inserted : ";
cin >> x; std::cin >> x;
L.insert(x); L.insert(x);
break; break;
case 2: case 2:
cout << "\nEnter the element to be removed : "; std::cout << "\nEnter the element to be removed : ";
cin >> x; std::cin >> x;
L.Remove(x); L.Remove(x);
break; break;
case 3: case 3:
cout << "\nEnter the element to be searched : "; std::cout << "\nEnter the element to be searched : ";
cin >> x; std::cin >> x;
L.Search(x); L.Search(x);
break; break;
case 4: case 4:

View File

@ -74,6 +74,10 @@ bool deleteString(trie* root, std::string str, int index) {
return true; return true;
} }
} }
/* should not return here */
std::cout << __func__ << ":" << __LINE__ << "Should not reach this line\n";
return false;
} }
int main() { int main() {