Expansion: A version of quicksort for wordlists

It is intended to be used with the full version not the direct quicksort
as the direct one is not currently correctly protected against end index
being out of bounds.
This commit is contained in:
Jérôme Guélen 2025-03-10 12:37:50 +01:00
parent 95d9f6282a
commit e7f12b54e9
No known key found for this signature in database
4 changed files with 128 additions and 9 deletions

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wordlist_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
/* Updated: 2025/03/10 12:23:36 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordlist.h"
#include <stdlib.h>
/*
** Returns the number of words present in the wordlist given as parameter.
** NOTE: Assumes that that 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);
}