2025-03-10 12:37:50 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* wordlist_utils.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
|
2025-03-19 17:20:03 +01:00
|
|
|
/* Updated: 2025/03/19 13:43:24 by jguelen ### ########.fr */
|
2025-03-10 12:37:50 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "wordlist.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Returns the number of words present in the wordlist given as parameter.
|
2025-03-19 17:20:03 +01:00
|
|
|
** NOTE: Assumes list not to be circular.
|
2025-03-10 12:37:50 +01:00
|
|
|
*/
|
|
|
|
|
int wordlist_size(t_wordlist *wordlist)
|
|
|
|
|
{
|
|
|
|
|
int size;
|
|
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
|
while (wordlist)
|
|
|
|
|
{
|
|
|
|
|
size++;
|
|
|
|
|
wordlist = wordlist->next;
|
|
|
|
|
}
|
|
|
|
|
return (size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t_wordlist *wordlist_last(t_wordlist *list)
|
|
|
|
|
{
|
|
|
|
|
if (!list)
|
|
|
|
|
return (NULL);
|
|
|
|
|
while (list->next)
|
|
|
|
|
list = list->next;
|
|
|
|
|
return (list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Returns the node at index idx in list or NULL if idx is out
|
|
|
|
|
** of bounds.
|
|
|
|
|
*/
|
|
|
|
|
t_wordlist *wordlist_get_elem(t_wordlist *list, int idx)
|
|
|
|
|
{
|
|
|
|
|
if (list == NULL || idx < 0)
|
|
|
|
|
return (NULL);
|
|
|
|
|
while (idx > 0 && list != NULL)
|
|
|
|
|
{
|
|
|
|
|
list = list->next;
|
|
|
|
|
idx--;
|
|
|
|
|
}
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return (NULL);
|
|
|
|
|
return (list);
|
2025-03-10 17:22:38 +01:00
|
|
|
}
|