-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_pop_tail.c
executable file
·88 lines (84 loc) · 3.04 KB
/
list_pop_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_pop_tail.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/24 21:20:39 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_pop_tail -- removes the last element of a list and returns
** the item it contains.
**
** If (*tail) points to anywhere other than the
** last element, (*tail), after popping, will be
** made to point to the new last element.
**
** If (*tail) points to the last element of a list,
** (*tail), after popping, will be made to point to
** NULL.
**
** This function can only be used ONCE with a
** pointer to a pointer that points to the last
** element of a list and TWICE with one that points
** to anywhere else.
**
** SYNOPSIS
** #include <../libft.h>
**
** void *
** list_pop_tail(t_list **tail);
**
** PARAMETERS
**
** t_list **tail Pointer to a pointer to any
** element of the list other
** than the last element.
**
** DESCRIPTION
** Removes and frees the last element of the list, that (*tail)
** points to, updates the new last element of the list to point
** to NULL and (*tail) to point to the new last element of the
** list. Finally, it returns the item that was contained by the
** last element of the list.
**
** If the list had a single element, (*tail), after popping the
** last element, will be made to point to NULL.
**
** RETURN VALUES
** If successful returns the item from the popped element of the
** list; otherwise NULL.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/list.h"
void *list_pop_tail(t_list **tail)
{
void *item;
t_list *current;
t_list *previous;
if (tail && (*tail))
{
previous = NULL;
current = (*tail);
while (current->next)
{
previous = current;
current = current->next;
}
if (previous)
{
previous->next = current->next;
(*tail) = previous;
}
else
(*tail) = NULL;
item = current->item;
free(current);
return (item);
}
return (NULL);
}