aboutsummaryrefslogtreecommitdiff
path: root/c12/ex15/ft_list_reverse_fun.c
blob: ffbeea22da583509ee65479c27f6880d528d2747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_list_reverse_fun.c                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <charles.cabergs@gmail.com>       +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/07/19 13:55:34 by cacharle          #+#    #+#             */
/*   Updated: 2019/07/23 15:44:23 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include "ft_list.h"

static t_list	*my_at(t_list *begin_list, int nbr)
{
	while (nbr-- > 0 && begin_list != NULL)
		begin_list = begin_list->next;
	return (begin_list);
}

static int		my_size(t_list *begin_list)
{
	int	counter;

	counter = 0;
	while (begin_list->next)
	{
		counter++;
		begin_list = begin_list->next;
	}
	return (counter);
}

void			ft_list_reverse_fun(t_list *begin_list)
{
	void			*tmp;
	unsigned int	j;
	unsigned int	i;

	j = my_size(begin_list);
	i = 0;
	while (i < j)
	{
		tmp = my_at(begin_list, i)->data;
		my_at(begin_list, i)->data = my_at(begin_list, j)->data;
		my_at(begin_list, j)->data = tmp;
		i++;
		j--;
	}
}