From 17e5db78e39ac072f62f0950d28ca4d28cf265ef Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Sun, 22 Mar 2026 00:29:38 -0600 Subject: [PATCH] Ok i forgot the menu, all done i guess, now yes do doxyfile --- include/linkedlist.h | 72 ++++++++++++++++++++++ src/main.cpp | 140 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 211 insertions(+), 1 deletion(-) diff --git a/include/linkedlist.h b/include/linkedlist.h index ce7fa29..f3edeec 100644 --- a/include/linkedlist.h +++ b/include/linkedlist.h @@ -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 + */ +[[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 : std::formatter { + + auto format(LinkedListErr err, format_context& ctx) const { + return std::formatter::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 @@ -431,4 +488,19 @@ bool LinkedList::is_empty() { return this->len() < 1; } +template +void LinkedList::clear() { + Node* current = head; + + while (current != nullptr) { + Node* next = current->next; + delete current; + current = next; + } + + head = nullptr; + tail = nullptr; + size = 0; +} + #endif // ! diff --git a/src/main.cpp b/src/main.cpp index dcf36c6..307f492 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,146 @@ #include "linkedlist.h" +#include "utils.h" #include #include +void menu(LinkedList& list); + int main(void) { - std::print("hi"); + LinkedList list = {}; + + menu(list); + return EXIT_SUCCESS; } + +void menu(LinkedList& 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(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; + } + } +}