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 <print>
/**
* @brief Concept that ensures a type supports equality comparison via operator==.
*
* @tparam T Type to be checked.
*/
template <typename T>
concept HasEqual = requires (T a, T b) {
{ 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>
concept LinkedListSupported =
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 {
LINKEDLIST_OK, /**< Operacin exitosa */
LINKEDLIST_BAD_ALLOC, /**< Error al asignar memoria */
LINKEDLIST_OUT_OF_BOUNDS, /**< Índice fuera de rango */
LINKESLIST_INVALID_SIZE, /**< Tamaño inválido */
LINKEDLIST_NOT_FOUND, /**< Elemento no encontrado */
LINKEDLIST_EMPTY, /**< La lista está vacia */
LINKEDLIST_OK, /**< Operation completed successfully */
LINKEDLIST_BAD_ALLOC, /**< Memory allocation failed */
LINKEDLIST_OUT_OF_BOUNDS, /**< Index is out of valid range */
LINKEDLIST_INVALID_SIZE, /**< Invalid size provided */
LINKEDLIST_NOT_FOUND, /**< Element not found */
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>
class Node {
public:
T data; /**< Dato almacenado en el nodo */
Node<T> *next; /**< Puntero al siguiente nodo */
T data; /**< Data stored in the node */
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
* utilizando nodos enlazados mediante punteros.
* A dynamic data structure that stores elements of type T
* 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>
class LinkedList {
private:
size_t size; /**< Numero de elementos en la lista */
Node<T> *head; /**< Primer nodo de la lista */
Node<T> *tail; /**< Último nodo de la lista */
size_t size; /**< Number of elements in the list */
Node<T>* head; /**< Pointer to the first node */
Node<T>* tail; /**< Pointer to the last node */
public:
/**
* @brief Constructor de la lista enlazada
* @brief Constructs an empty linked list.
*/
LinkedList();
/**
* @brief Destructor de la lista
* @brief Destroys the linked list.
*
* Libera toda la memoria de los nodos.
* Releases all dynamically allocated nodes.
*/
~LinkedList();
/**
* @brief Obtiene el elemento en una posicion específica
* @brief Retrieves the element at a given index.
*
* @param index indice del elemento
* @return std::expected<T, LinkedListErr> Valor encontrado o error
* @param index Position of the element.
* @return std::expected<T, LinkedListErr> The element or an error code.
*/
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
* @return LinkedListErr Cdigo de estado de la operacion
* @param value Value to insert.
* @return LinkedListErr Status code of the operation.
*/
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
* @return LinkedListErr Codigo de estado
* @param value Value to insert.
* @return LinkedListErr Status code of the operation.
*/
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();
/**
* @brief Elimina el elemento en una posicion especifica
* @brief Removes the element at a specific index.
*
* @param index Indice del elemento a eliminar
* @return LinkedListErr Codigo de estado
* @param index Index of the element to remove.
* @return LinkedListErr Status code.
*/
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 value Nuevo valor
* @return LinkedListErr Código de estado
* @param index Index of the element.
* @param value New value.
* @return LinkedListErr Status code.
*/
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 value Nuevo valor
* @return LinkedListErr Codigo de estado
* @param index Target position.
* @param value Value to insert.
* @return LinkedListErr Status code.
*/
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
* @return std::expected<size_t, LinkedListErr> Índice del elemento o error
* @param value Value to search for.
* @return std::expected<size_t, LinkedListErr> Index of the element or error.
*/
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();
/**
* @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();
/**
* @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();
};