Ok i forgot the menu, all done i guess, now yes do doxyfile
This commit is contained in:
@@ -46,6 +46,54 @@ enum class LinkedListErr {
|
||||
LINKEDLIST_EMPTY, /**< The list is empty */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Converts a LinkedListErr value to its string representation.
|
||||
*
|
||||
* Provides a human-readable, null-terminated string corresponding to
|
||||
* the given LinkedListErr enumeration value. This function is primarily
|
||||
* intended for logging, debugging, and formatting purposes (e.g., with
|
||||
* std::format / std::print).
|
||||
*
|
||||
* @param err The LinkedListErr value to convert.
|
||||
*
|
||||
* @return const char* A pointer to a statically allocated string literal
|
||||
* representing the error. The returned pointer remains valid for the
|
||||
* lifetime of the program and must not be freed.
|
||||
*
|
||||
* @note This function does not throw exceptions.
|
||||
* @note If an unknown or invalid enumeration value is provided, the
|
||||
* string "UNKNOWN_ERROR" is returned.
|
||||
*
|
||||
* @see std::formatter<LinkedListErr>
|
||||
*/
|
||||
[[nodiscard]] inline const char* to_string(LinkedListErr err) {
|
||||
switch (err) {
|
||||
case LinkedListErr::LINKEDLIST_OK:
|
||||
return "OK";
|
||||
case LinkedListErr::LINKEDLIST_BAD_ALLOC:
|
||||
return "The program couldn't allocate memory for the list.";
|
||||
case LinkedListErr::LINKEDLIST_OUT_OF_BOUNDS:
|
||||
return "The index is out of bounds.";
|
||||
case LinkedListErr::LINKEDLIST_INVALID_SIZE:
|
||||
return "Is an invalid size fo the list.";
|
||||
case LinkedListErr::LINKEDLIST_NOT_FOUND:
|
||||
return "The value was not found.";
|
||||
case LinkedListErr::LINKEDLIST_EMPTY:
|
||||
return "The list is empty";
|
||||
default:
|
||||
return "UNKNOWN_ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
struct std::formatter<LinkedListErr> : std::formatter<const char*> {
|
||||
|
||||
auto format(LinkedListErr err, format_context& ctx) const {
|
||||
return std::formatter<const char*>::format(to_string(err), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Node of a singly linked list.
|
||||
*
|
||||
@@ -172,6 +220,15 @@ public:
|
||||
* @return size_t Number of stored elements.
|
||||
*/
|
||||
size_t len();
|
||||
|
||||
/**
|
||||
* @brief Clears the list by deleting all its elements.
|
||||
*
|
||||
* Frees all nodes and resets the list to an empty state.
|
||||
*
|
||||
* @note After this call, the list size is 0 and both head and tail are nullptr.
|
||||
*/
|
||||
void clear();
|
||||
};
|
||||
|
||||
template <LinkedListSupported T>
|
||||
@@ -431,4 +488,19 @@ bool LinkedList<T>::is_empty() {
|
||||
return this->len() < 1;
|
||||
}
|
||||
|
||||
template <LinkedListSupported T>
|
||||
void LinkedList<T>::clear() {
|
||||
Node<T>* current = head;
|
||||
|
||||
while (current != nullptr) {
|
||||
Node<T>* next = current->next;
|
||||
delete current;
|
||||
current = next;
|
||||
}
|
||||
|
||||
head = nullptr;
|
||||
tail = nullptr;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
#endif // !
|
||||
|
||||
140
src/main.cpp
140
src/main.cpp
@@ -1,8 +1,146 @@
|
||||
#include "linkedlist.h"
|
||||
#include "utils.h"
|
||||
#include <cstdlib>
|
||||
#include <print>
|
||||
|
||||
void menu(LinkedList<int>& list);
|
||||
|
||||
int main(void) {
|
||||
std::print("hi");
|
||||
LinkedList<int> list = {};
|
||||
|
||||
menu(list);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void menu(LinkedList<int>& list) {
|
||||
while (true) {
|
||||
clear_screen();
|
||||
std::println("--LIST-MANAGER--");
|
||||
std::println("1) Create List (Null init).");
|
||||
std::println("2) Add Ascendently (Insert at index)");
|
||||
std::println("3) Delete value.");
|
||||
std::println("4) Search value.");
|
||||
std::println("5) Print list.");
|
||||
std::println("6) Exit.");
|
||||
|
||||
int choice;
|
||||
if (!read_int("", &choice)) {
|
||||
std::println("Input is not valid.");
|
||||
}
|
||||
|
||||
clear_screen();
|
||||
switch (choice) {
|
||||
case 1: {
|
||||
list.clear();
|
||||
std::println("List was cleared and reset.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
int index;
|
||||
if (!read_int("Enter the index to insert a value: ", &index)) {
|
||||
std::println("Input is not valid.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
if (index < 0) {
|
||||
std::println("Negative index is not supported.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
if (static_cast<size_t>(index) > list.len()) {
|
||||
std::println("Index is out of bounds");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
int value;
|
||||
if (!read_int("Enter the value: ", &value)) {
|
||||
std::println("Invalid input.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
auto result = list.insert(index, value);
|
||||
if (result == LinkedListErr::LINKEDLIST_OK) {
|
||||
std::println("Value correctly added.");
|
||||
wait_enter();
|
||||
break;
|
||||
} else {
|
||||
std::println("{}", result);
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
case 3: {
|
||||
int value;
|
||||
if (!read_int("Enter the value to eliminate: ", &value)) {
|
||||
std::println("Invalid input.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
auto result = list.find(value);
|
||||
if (!result.has_value()) {
|
||||
std::println("{}", result.error());
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
list.remove(result.value());
|
||||
std::println(
|
||||
"The value {} at index {} was removed.",
|
||||
value,
|
||||
result.value());
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
int value;
|
||||
if (!read_int("Enter the value to search: ", &value)) {
|
||||
std::println("Invalid input.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
auto result = list.find(value);
|
||||
if (!result.has_value()) {
|
||||
std::println("{}", result.error());
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
|
||||
std::println(
|
||||
"The value {} was foud at index {}.",
|
||||
value,
|
||||
result.value());
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
if (list.is_empty()) {
|
||||
std::println("List is empty.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
list.print();
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
case 6: {
|
||||
std::println("Exiting...\nGoodbye:)");
|
||||
wait_enter();
|
||||
clear_screen();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
default:
|
||||
std::println("Invalid option.");
|
||||
wait_enter();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user