mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
[docs/test]: Documentation and test in the list_array.cpp (#1375)
* Added introductory documentation in list_array * documentation for search completed, minor bug fixed in the list_array * bug fixed in the insert method * edge case check added in the remove method * Documentation completed in list_array * Testing completed in the list_array.cpp * Bug fixed in the sort method * test cases improved and minor bug fixed in list_array * minor allignment changed * changes in documentation and improvement in the code in list_array * resolve clang-tidy warning * link added datatype changed * namespaces added * minor documentation changed in list_array.cpp Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
parent
28b5b2b829
commit
e813f9b2a6
@ -1,165 +1,250 @@
|
|||||||
/**
|
/**
|
||||||
* @file list_array.cpp
|
* @file
|
||||||
* @todo Add documentation
|
* @brief [Dynamic Array](https://en.wikipedia.org/wiki/Dynamic_array)
|
||||||
* @warning The sorting algorithm is erroneous
|
*
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* ### 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.
|
||||||
*/
|
*/
|
||||||
#include <array>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
struct list {
|
#include <iostream> /// for io operations
|
||||||
std::array<int, 50> data{};
|
#include <array> /// for std::array
|
||||||
int top = 0;
|
#include <cassert> /// for assert
|
||||||
bool isSorted = false;
|
|
||||||
|
|
||||||
int BinarySearch(const std::array<int, 50>& dataArr, int first, int last,
|
/**
|
||||||
int x) {
|
* @namespace data_structures
|
||||||
if (last < first) {
|
* @brief Algorithms with data structures
|
||||||
|
*/
|
||||||
|
namespace data_structures {
|
||||||
|
/**
|
||||||
|
* @namespace list_array
|
||||||
|
* @brief Functions for [Dynamic Array](https://en.wikipedia.org/wiki/Dynamic_array) algorithm
|
||||||
|
*/
|
||||||
|
namespace list_array {
|
||||||
|
/**
|
||||||
|
* @brief Structure of List with supporting methods.
|
||||||
|
*/
|
||||||
|
struct list {
|
||||||
|
std::array<uint64_t, 50> 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, 50> &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";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int mid = (first + last) / 2;
|
|
||||||
if (dataArr[mid] == x) {
|
|
||||||
return mid;
|
|
||||||
} 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;
|
* @brief Search an element using linear search
|
||||||
}
|
* @param dataArr list
|
||||||
|
* @param val element that will be searched
|
||||||
int LinearSearch(const std::array<int, 50>& dataArr, int x) const {
|
* @return index of element in the list if present else -1
|
||||||
for (int i = 0; i < top; i++) {
|
*/
|
||||||
if (dataArr[i] == x) {
|
uint64_t LinearSearch(const std::array<uint64_t, 50> &dataArr, const uint64_t &val) const {
|
||||||
return i;
|
// 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
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Search(int x) {
|
|
||||||
int pos = 0;
|
|
||||||
|
|
||||||
if (isSorted) {
|
|
||||||
pos = BinarySearch(data, 0, top - 1, x);
|
|
||||||
} else {
|
|
||||||
pos = LinearSearch(data, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pos != -1) {
|
|
||||||
std::cout << "\nElement found at position : " << pos;
|
|
||||||
} else {
|
|
||||||
std::cout << "\nElement not found";
|
|
||||||
}
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Sort() {
|
|
||||||
int i = 0, j = 0, pos = 0;
|
|
||||||
for (i = 0; i < top; i++) {
|
|
||||||
int min = data[i];
|
|
||||||
for (j = i + 1; j < top; j++) {
|
|
||||||
if (data[j] < min) {
|
|
||||||
pos = j;
|
|
||||||
min = data[pos];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// element is not present in the list
|
||||||
int temp = data[i];
|
return -1;
|
||||||
data[i] = data[pos];
|
|
||||||
data[pos] = temp;
|
|
||||||
}
|
}
|
||||||
isSorted = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(int x) {
|
/*
|
||||||
if (!isSorted) {
|
* @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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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 == 49) {
|
if (top == 49) {
|
||||||
std::cout << "\nOverflow";
|
std::cout << "\nOverflow";
|
||||||
} else {
|
return;
|
||||||
data[top] = x;
|
}
|
||||||
|
// if list is not sorted, insert at the last
|
||||||
|
// otherwise place it to correct position
|
||||||
|
if (!isSorted) {
|
||||||
|
data[top] = val;
|
||||||
top++;
|
top++;
|
||||||
}
|
} else {
|
||||||
} else {
|
uint64_t pos = 0; // Initialize the index variable
|
||||||
int pos = 0;
|
// Going through each element and find correct position for element
|
||||||
|
for (uint64_t i = 0; i < top - 1; i++) {
|
||||||
for (int i = 0; i < top - 1; i++) {
|
// check for the correct position
|
||||||
if (data[i] <= x && x <= data[i + 1]) {
|
if (data[i] <= val && val <= data[i + 1]) {
|
||||||
pos = i + 1;
|
pos = i + 1; // assign correct pos to the index var
|
||||||
break;
|
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];
|
||||||
|
}
|
||||||
|
top++; // Increment the value of top.
|
||||||
|
data[pos] = val; // Assign the value to the correct index in the array
|
||||||
}
|
}
|
||||||
if (pos == 0) {
|
}
|
||||||
pos = top - 1;
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
for (int i = top; i > pos; i--) {
|
// shift all the element 1 left to fill vacant space
|
||||||
data[i] = data[i - 1];
|
for (uint64_t i = pos; i < top; i++) {
|
||||||
|
data[i] = data[i + 1];
|
||||||
}
|
}
|
||||||
top++;
|
top--; // decrement the top variable to maintain last index
|
||||||
data[pos] = x;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Remove(int x) {
|
/**
|
||||||
int pos = Search(x);
|
* @brief Utility function to print array
|
||||||
std::cout << "\n" << data[pos] << " deleted";
|
* @returns void
|
||||||
for (int i = pos; i < top; i++) {
|
*/
|
||||||
data[i] = data[i + 1];
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
top--;
|
}; // structure list
|
||||||
}
|
} // namespace list_array
|
||||||
|
} // namespace data_structures
|
||||||
|
|
||||||
void Show() {
|
/**
|
||||||
for (int i = 0; i < top; i++) {
|
* @brief Test implementations
|
||||||
std::cout << data[i] << "\t";
|
* @returns void
|
||||||
}
|
*/
|
||||||
}
|
static void test() {
|
||||||
};
|
data_structures::list_array::list L;
|
||||||
|
|
||||||
|
// Insert testing
|
||||||
|
L.insert(11);
|
||||||
|
L.insert(12);
|
||||||
|
assert(L.top == 2);
|
||||||
|
L.insert(15);
|
||||||
|
L.insert(10);
|
||||||
|
L.insert(12);
|
||||||
|
L.insert(20);
|
||||||
|
L.insert(18);
|
||||||
|
assert(L.top == 7);
|
||||||
|
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
|
||||||
|
assert(L.top == 5);
|
||||||
|
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(12) == 2);
|
||||||
|
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(12) == 2);
|
||||||
|
assert(L.search(50) == -1); // search for the non-existing element
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @returns 0 on exit
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
list L;
|
test(); // Execute the tests
|
||||||
int choice = 0;
|
|
||||||
int x = 0;
|
|
||||||
do {
|
|
||||||
// Choices for operations on the list_array.
|
|
||||||
std::cout << "\n0.Exit";
|
|
||||||
std::cout << "\n1.Insert";
|
|
||||||
std::cout << "\n2.Delete";
|
|
||||||
std::cout << "\n3.Search";
|
|
||||||
std::cout << "\n4.Sort";
|
|
||||||
std::cout << "\n5.Print";
|
|
||||||
std::cout << "\n\nEnter Your Choice : ";
|
|
||||||
std::cin >> choice;
|
|
||||||
switch (choice) {
|
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user