-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_utils.c
More file actions
63 lines (56 loc) · 1.88 KB
/
stack_utils.c
File metadata and controls
63 lines (56 loc) · 1.88 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gpouzet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 22:23:07 by gpouzet #+# #+# */
/* Updated: 2023/02/23 06:10:40 by gpouzet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "push_swap.h"
void stack_push(t_stack *stack, int item)
{
if (stack_full(stack))
return ;
stack->array[++stack->top] = item;
}
void stack_swap(t_stack *stack)
{
int tmp1;
int tmp2;
tmp1 = stack_pop(stack);
tmp2 = stack_pop(stack);
stack_push(stack, tmp1);
stack_push(stack, tmp2);
}
void stack_rotate(t_stack *stack)
{
int tmp;
t_stack *stacktmp;
stacktmp = construct_stack(stack->capacity);
tmp = stack_pop(stack);
while (stack->top > -1)
stack_push(stacktmp, stack_pop(stack));
stack_push(stack, tmp);
while (!stack_empty(stacktmp))
stack_push(stack, stack_pop(stacktmp));
free(stacktmp->array);
free(stacktmp);
}
void stack_rev_rota(t_stack *stack)
{
int tmp;
t_stack *stacktmp;
stacktmp = construct_stack(stack->capacity);
while (stack->top > -1)
stack_push(stacktmp, stack_pop(stack));
tmp = stack_pop(stacktmp);
while (!stack_empty(stacktmp))
stack_push(stack, stack_pop(stacktmp));
stack_push(stack, tmp);
free(stacktmp->array);
free(stacktmp);
}