push_swap/operations.c
2022-12-07 20:13:22 +01:00

61 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/07 18:36:09 by erey-bet #+# #+# */
/* Updated: 2022/12/07 18:36:10 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void swap(t_stack *s, char *message)
{
long c;
c = (*s).list[0];
(*s).list[0] = (*s).list[1];
(*s).list[1] = c;
write(1, "s", 1);
write(1, message, 1);
write(1, "\n", 1);
}
void push(t_stack *s_push, t_stack *s_receive, char *message)
{
int i;
long push;
i = 0;
push = (*s_push).list[0];
while (++i < (*s_push).len)
(*s_push).list[i - 1] = (*s_push).list[i];
i = (*s_receive).len;
while (i-- > 0)
(*s_receive).list[i + 1] = (*s_receive).list[i];
(*s_receive).list[0] = push;
(*s_receive).len += 1;
(*s_push).len -= 1;
write(1, "p", 1);
write(1, message, 1);
write(1, "\n", 1);
}
void rotate(t_stack *s, char *message)
{
int i;
long rotate;
i = 0;
rotate = (*s).list[0];
while (++i < (*s).len)
(*s).list[i - 1] = (*s).list[i];
(*s).list[(*s).len - 1] = rotate;
write(1, "r", 1);
write(1, message, 1);
write(1, "\n", 1);
}