Corrected comments, now just need tests

This commit is contained in:
2026-03-21 22:58:04 -06:00
parent c38f430940
commit 52c7e3dfff

View File

@@ -8,11 +8,25 @@
#include <new> #include <new>
#include <print> #include <print>
/**
* @brief Concept that ensures a type supports equality comparison via operator==.
*
* @tparam T Type to be checked.
*/
template <typename T> template <typename T>
concept HasEqual = requires (T a, T b) { concept HasEqual = requires (T a, T b) {
{a == b} -> std::convertible_to<bool>; { a == b } -> std::convertible_to<bool>;
}; };
/**
* @brief Concept defining the requirements for types supported by LinkedList.
*
* A valid type must:
* - Be formattable (usable with std::print / std::format)
* - Support equality comparison
*
* @tparam T Type to be checked.
*/
template <typename T> template <typename T>
concept LinkedListSupported = concept LinkedListSupported =
std::formattable<T, char> && std::formattable<T, char> &&
@@ -21,141 +35,141 @@ concept LinkedListSupported =
/** /**
* @brief Codigos de error para operaciones de LinkedList * @brief Error codes for LinkedList operations.
*/ */
enum class LinkedListErr { enum class LinkedListErr {
LINKEDLIST_OK, /**< Operacin exitosa */ LINKEDLIST_OK, /**< Operation completed successfully */
LINKEDLIST_BAD_ALLOC, /**< Error al asignar memoria */ LINKEDLIST_BAD_ALLOC, /**< Memory allocation failed */
LINKEDLIST_OUT_OF_BOUNDS, /**< Índice fuera de rango */ LINKEDLIST_OUT_OF_BOUNDS, /**< Index is out of valid range */
LINKESLIST_INVALID_SIZE, /**< Tamaño inválido */ LINKEDLIST_INVALID_SIZE, /**< Invalid size provided */
LINKEDLIST_NOT_FOUND, /**< Elemento no encontrado */ LINKEDLIST_NOT_FOUND, /**< Element not found */
LINKEDLIST_EMPTY, /**< La lista está vacia */ LINKEDLIST_EMPTY, /**< The list is empty */
}; };
/** /**
* @brief Nodo de una lista enlazada * @brief Node of a singly linked list.
* *
* @tparam T Tipo de dato almacenado en el nodo * @tparam T Type of data stored in the node.
*/ */
template<LinkedListSupported T> template<LinkedListSupported T>
class Node { class Node {
public: public:
T data; /**< Dato almacenado en el nodo */ T data; /**< Data stored in the node */
Node<T> *next; /**< Puntero al siguiente nodo */ Node<T>* next; /**< Pointer to the next node */
}; };
/** /**
* @brief Implementación de una lista enlazada simple * @brief Implementation of a singly linked list.
* *
* Estructura de datos dinamica que almacena elementos de tipo T * A dynamic data structure that stores elements of type T
* utilizando nodos enlazados mediante punteros. * using nodes connected via pointers.
* *
* @tparam T Tipo de dato almacenado en la lista * @tparam T Type of elements stored in the list.
*/ */
template<LinkedListSupported T> template<LinkedListSupported T>
class LinkedList { class LinkedList {
private: private:
size_t size; /**< Numero de elementos en la lista */ size_t size; /**< Number of elements in the list */
Node<T> *head; /**< Primer nodo de la lista */ Node<T>* head; /**< Pointer to the first node */
Node<T> *tail; /**< Último nodo de la lista */ Node<T>* tail; /**< Pointer to the last node */
public: public:
/** /**
* @brief Constructor de la lista enlazada * @brief Constructs an empty linked list.
*/ */
LinkedList(); LinkedList();
/** /**
* @brief Destructor de la lista * @brief Destroys the linked list.
* *
* Libera toda la memoria de los nodos. * Releases all dynamically allocated nodes.
*/ */
~LinkedList(); ~LinkedList();
/** /**
* @brief Obtiene el elemento en una posicion específica * @brief Retrieves the element at a given index.
* *
* @param index indice del elemento * @param index Position of the element.
* @return std::expected<T, LinkedListErr> Valor encontrado o error * @return std::expected<T, LinkedListErr> The element or an error code.
*/ */
std::expected<T, LinkedListErr> get(size_t index); std::expected<T, LinkedListErr> get(size_t index);
/** /**
* @brief Inserta un elemento al final de la lista * @brief Appends an element to the end of the list.
* *
* @param value Valor a insertar * @param value Value to insert.
* @return LinkedListErr Cdigo de estado de la operacion * @return LinkedListErr Status code of the operation.
*/ */
LinkedListErr append(const T& value); LinkedListErr append(const T& value);
/** /**
* @brief Inserta un elemento al inicio de la lista * @brief Inserts an element at the beginning of the list.
* *
* @param value Valor a insertar * @param value Value to insert.
* @return LinkedListErr Codigo de estado * @return LinkedListErr Status code of the operation.
*/ */
LinkedListErr prepend(const T& value); LinkedListErr prepend(const T& value);
/** /**
* @brief Elimina y devuelve el ultimo elemento de la lista * @brief Removes and returns the last element of the list.
* *
* @return std::expected<T, LinkedListErr> Elemento eliminado o error * @return std::expected<T, LinkedListErr> Removed element or an error.
*/ */
std::expected<T, LinkedListErr> pop(); std::expected<T, LinkedListErr> pop();
/** /**
* @brief Elimina el elemento en una posicion especifica * @brief Removes the element at a specific index.
* *
* @param index Indice del elemento a eliminar * @param index Index of the element to remove.
* @return LinkedListErr Codigo de estado * @return LinkedListErr Status code.
*/ */
LinkedListErr remove(size_t index); LinkedListErr remove(size_t index);
/** /**
* @brief Modifica el valor de un elemento en un indice * @brief Updates the value of an element at a given index.
* *
* @param index Índice del elemento * @param index Index of the element.
* @param value Nuevo valor * @param value New value.
* @return LinkedListErr Código de estado * @return LinkedListErr Status code.
*/ */
LinkedListErr set(size_t index, const T& value); LinkedListErr set(size_t index, const T& value);
/** /**
* @brief Inserta un valor en el index dado. * @brief Inserts a value at a specified index.
* *
* @param index Indice del elemento * @param index Target position.
* @param value Nuevo valor * @param value Value to insert.
* @return LinkedListErr Codigo de estado * @return LinkedListErr Status code.
*/ */
LinkedListErr insert (size_t index, const T& value); LinkedListErr insert(size_t index, const T& value);
/** /**
* @brief Busca un valor dentro de la lista * @brief Searches for a value in the list.
* *
* @param value Valor a buscar * @param value Value to search for.
* @return std::expected<size_t, LinkedListErr> Índice del elemento o error * @return std::expected<size_t, LinkedListErr> Index of the element or error.
*/ */
std::expected<size_t, LinkedListErr> find(const T& value); std::expected<size_t, LinkedListErr> find(const T& value);
/** /**
* @brief Imprime los elementos de la lista * @brief Prints all elements in the list.
* *
* @return LinkedListErr Código de estado * @return LinkedListErr Status code.
*/ */
LinkedListErr print(); LinkedListErr print();
/** /**
* @brief Verifica si la lista esta vacia * @brief Checks whether the list is empty.
* *
* @return true si la lista no contiene elementos * @return true if the list contains no elements, false otherwise.
*/ */
bool is_empty(); bool is_empty();
/** /**
* @brief Obtiene el numero de elementos de la lista * @brief Returns the number of elements in the list.
* *
* @return size_t Tamaño de la lista * @return size_t Number of stored elements.
*/ */
size_t len(); size_t len();
}; };