69 lines
1.7 KiB
C
69 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_reverse_fun.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/07/05 17:46:12 by whaffman #+# #+# */
|
|
/* Updated: 2024/07/05 18:00:33 by whaffman ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ft_list.h"
|
|
|
|
void ft_swap_ptr(void **ptr1, void **ptr2)
|
|
{
|
|
void *temp;
|
|
|
|
temp = *ptr1;
|
|
*ptr1 = *ptr2;
|
|
*ptr2 = temp;
|
|
}
|
|
|
|
int ft_list_size(t_list *begin_list)
|
|
{
|
|
int count;
|
|
|
|
count = 1;
|
|
if (!begin_list)
|
|
return (0);
|
|
while (begin_list->next)
|
|
{
|
|
begin_list = begin_list->next;
|
|
count++;
|
|
}
|
|
return (count);
|
|
}
|
|
|
|
t_list *ft_list_at(t_list *begin_list, unsigned int nbr)
|
|
{
|
|
while (nbr > 0)
|
|
{
|
|
begin_list = begin_list->next;
|
|
if (!begin_list)
|
|
return (NULL);
|
|
nbr--;
|
|
}
|
|
return (begin_list);
|
|
}
|
|
|
|
void ft_list_reverse_fun(t_list *begin_list)
|
|
{
|
|
int front;
|
|
int back;
|
|
t_list *front_elem;
|
|
t_list *back_elem;
|
|
|
|
front = 0;
|
|
back = ft_list_size(begin_list) - 1;
|
|
while (front < back)
|
|
{
|
|
front_elem = ft_list_at(begin_list, front);
|
|
back_elem = ft_list_at(begin_list, back);
|
|
ft_swap_ptr(&front_elem->data, &back_elem->data);
|
|
front++;
|
|
back--;
|
|
}
|
|
}
|