-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
66 lines (58 loc) · 1.85 KB
/
stack.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/07 20:46:14 by alex #+# #+# */
/* Updated: 2020/05/05 16:16:49 by Alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/lem_in.h"
t_stack *stack_init(void)
{
t_stack *stack;
stack = ft_malloc_or_exit(sizeof(t_stack));
stack->elements = NULL;
return (stack);
}
void stack_push(t_stack *stack, t_room *room)
{
t_list *element;
element = stack->elements;
while (element != NULL)
{
if (element->data == room)
return ;
element = element->next;
}
list_push(&stack->elements, room);
}
t_room *stack_pop_min(t_stack *stack)
{
t_list **addr;
t_list **addr_min;
int min_distance;
if (stack->elements == NULL)
return (NULL);
addr = &stack->elements;
addr_min = addr;
min_distance = ((t_room *)(*addr)->data)->distance;
while (*addr != NULL)
{
if (((t_room *)(*addr)->data)->distance < min_distance)
{
addr_min = addr;
min_distance = ((t_room *)(*addr)->data)->distance;
}
addr = &(*addr)->next;
}
return (list_pop(addr_min));
}
void stack_free(t_stack *stack)
{
while (stack->elements != NULL)
(void)list_pop(&stack->elements);
free(stack);
}