lst sort
This commit is contained in:
parent
10ffa678ac
commit
0d10f17f9d
2 changed files with 38 additions and 0 deletions
37
libft/ft_lst_sort.c
Normal file
37
libft/ft_lst_sort.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "libft.h"
|
||||
|
||||
static void ft_lst_swap(t_list **current)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = (*current)->next->next;
|
||||
(*current)->next->next = (*current);
|
||||
(*current)->next = tmp;
|
||||
}
|
||||
|
||||
void ft_lst_sort(t_list **begin_list, int (*cmp)())
|
||||
{
|
||||
t_list *current;
|
||||
t_list *last;
|
||||
|
||||
current = *begin_list;
|
||||
if (!*begin_list)
|
||||
return ;
|
||||
while (current->next)
|
||||
{
|
||||
if ((*cmp)(current->content, current->next->content) > 0)
|
||||
{
|
||||
if (current != *begin_list)
|
||||
last->next = current->next;
|
||||
else
|
||||
*begin_list = current->next;
|
||||
ft_lst_swap(¤t);
|
||||
current = *begin_list;
|
||||
}
|
||||
else
|
||||
{
|
||||
last = current;
|
||||
current = current->next ? current->next : current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -81,6 +81,7 @@ void ft_lstadd(t_list **alst, t_list *new);
|
|||
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
|
||||
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
|
||||
|
||||
void ft_lst_sort(t_list **begin_list, int (*cmp)());
|
||||
void ft_lst_print(t_list *list, void (*printer)());
|
||||
int ft_lstsize(t_list *lst);
|
||||
void ft_lsteadd(t_list **alst, t_list *new);
|
||||
|
|
|
|||
Loading…
Reference in a new issue