-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_pop.c
executable file
·69 lines (65 loc) · 2.16 KB
/
list_pop.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_pop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 19:30:38 by akharrou #+# #+# */
/* Updated: 2019/03/04 13:17:20 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_pop -- removes the last element of a list and returns the item
** it contains.
**
** SYNOPSIS
** #include <../libft.h>
**
** void *
** list_pop(t_list **head);
**
** PARAMETERS
**
** t_list **head Pointer to a pointer to the first
** element of a list.
**
** DESCRIPTION
** Removes and frees the last element of the list, that (*head)
** points to, updates the NEW last element of the list to point
** to NULL and returns the item it contains.
**
** If the list had a single element, (*head), 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(t_list **head)
{
void *item;
t_list *current;
t_list *previous;
if (head && (*head))
{
previous = NULL;
current = (*head);
while (current->next)
{
previous = current;
current = current->next;
}
if (previous)
previous->next = current->next;
else
(*head) = NULL;
item = current->item;
free(current);
return (item);
}
return (NULL);
}