diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-19 08:38:07 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-19 08:38:07 +0100 |
| commit | 2b4327b7a448228f67a054b4bdaa3f84b9db2164 (patch) | |
| tree | 154e15f5533dee898f180e3588165c06b43a76ec /src/common/stack_op.c | |
| parent | 5e0e41652315114a8b0d883c473dbbbfc1c28342 (diff) | |
| download | push_swap-2b4327b7a448228f67a054b4bdaa3f84b9db2164.tar.gz push_swap-2b4327b7a448228f67a054b4bdaa3f84b9db2164.tar.bz2 push_swap-2b4327b7a448228f67a054b4bdaa3f84b9db2164.zip | |
refactored stack functions and checking for duplicates in args
Diffstat (limited to 'src/common/stack_op.c')
| -rw-r--r-- | src/common/stack_op.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/common/stack_op.c b/src/common/stack_op.c new file mode 100644 index 0000000..e1c095b --- /dev/null +++ b/src/common/stack_op.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 06:37:44 by cacharle #+# #+# */ +/* Updated: 2020/01/19 07:06:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +void stack_swap(t_stack *stack) +{ + int first; + int second; + + if (stack_length(stack) < 2) + return ; + first = stack_peek(stack); + stack_pop(stack); + second = stack_peek(stack); + stack_pop(stack); + stack_push(stack, first); + stack_push(stack, second); +} + +void stack_push_to(t_stack *from, t_stack *to) +{ + int tmp; + + if (stack_empty(from)) + return ; + tmp = stack_peek(from); + stack_pop(from); + stack_push(to, tmp); +} + +void stack_rotate(t_stack *stack) +{ + int tmp; + + if (stack_length(stack) < 2) + return ; + tmp = stack_peek(stack); + ft_memmove(stack->elements + 1, stack->elements, + (stack_length(stack) - 1) * sizeof(int)); + stack->elements[0] = tmp; +} + +void stack_reverse_rotate(t_stack *stack) +{ + int tmp; + + if (stack_length(stack) < 2) + return ; + tmp = stack->elements[0]; + ft_memmove(stack->elements, stack->elements + 1, + (stack_length(stack) - 1) * sizeof(int)); + stack->elements[stack->top] = tmp; +} |
