-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_append_tail.c
executable file
·70 lines (66 loc) · 2.35 KB
/
list_append_tail.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
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_append_tail.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 16:33:08 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_append_tail -- adds a new element to the end of a list and
** (*tail) is made to point to the last element
** of the list.
**
** SYNOPSIS
** #include "list.h"
**
** t_list *
** list_append_tail(t_list **tail, const void *item);
**
** PARAMETERS
**
** t_list **tail Pointer to a pointer to the
** last element of the list (or
** any element of the list).
**
** const void *item Data that will be stored in
** the new list element.
**
** DESCRIPTION
** Creates a new list element, storing 'item' as its item, then
** adds the element to the end of the list (that (*tail) points
** to).
**
** If the list does not exist the newly created element is made
** to be the last/first element of the list.
**
** (*tail) is made to point to the new last element of the list.
**
** RETURN VALUES
** Returns 0 if successful; otherwise -1.
*/
#include "../Includes/list.h"
int list_append_tail(t_list **tail, const void *item)
{
t_list *probe;
t_list *new_elem;
if (tail && (new_elem = list_newelem(item)))
{
if (*tail)
{
probe = (*tail);
while (probe->next)
probe = probe->next;
probe->next = new_elem;
(*tail) = probe->next;
}
else
(*tail) = new_elem;
return (0);
}
return (-1);
}