-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_elem_at.c
executable file
·62 lines (58 loc) · 1.92 KB
/
list_elem_at.c
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
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_elem_at.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 19:28:23 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_elem_at -- retrieve the i'th element of a list.
**
** SYNOPSIS
** #include <../libft.h>
**
** t_list *
** list_elem_at(t_list *head, unsigned int i);
**
** PARAMETERS
**
** t_list *head Pointer to the first element of
** a list.
**
** unsigned int i Index at which to find the element.
**
** DESCRIPTION
** Retrieves the list element found at the i'th index in the
** list that 'head' points to.
**
** If the given index goes past the end of the list, then NULL is
** returned.
**
** RETURN VALUES
** If successful returns the element found at the specified index;
** otherwise NULL.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/list.h"
t_list *list_elem_at(t_list *head, unsigned int i)
{
unsigned int index;
if (head)
{
index = 0;
while (i > index)
{
if (!(head->next))
return (NULL);
head = head->next;
++index;
}
return (head);
}
return (NULL);
}