From 2b4327b7a448228f67a054b4bdaa3f84b9db2164 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 19 Jan 2020 08:38:07 +0100 Subject: refactored stack functions and checking for duplicates in args --- src/common/stack_op.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/common/stack_op.c (limited to 'src/common/stack_op.c') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} -- cgit