minishell/src/parser/wordlist/wordlist_utils.c

59 lines
1.6 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wordlist_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
/* Updated: 2025/03/19 13:43:24 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include <stdlib.h>
/*
** Returns the number of words present in the wordlist given as parameter.
** NOTE: Assumes list not to be circular.
*/
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
}