Compare commits

...

4 Commits

Author SHA1 Message Date
hsv2 ba33cb63c8 push swap specific func wip 2022-09-24 23:07:27 +02:00
hsv2 87443af3c8 test swap 2022-09-24 23:07:03 +02:00
hsv2 cc5fb798c1 remove trash file 2022-09-24 23:06:43 +02:00
hsv2 38a47043af fix stack_push add missing stack_swap 2022-09-24 23:05:49 +02:00
8 changed files with 190 additions and 6 deletions

39
include/push_swap.h Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H
# include <ft_stack.h>
typedef struct s_push_swap_ctx
{
unsigned int instruction_counter;
t_stack *stack_a;
t_stack *stack_b;
} t_push_swap_ctx;
void push_swap_pa(t_push_swap_ctx *ctx);
void push_swap_pb(t_push_swap_ctx *ctx);
void push_swap_ra(t_push_swap_ctx *ctx);
void push_swap_rb(t_push_swap_ctx *ctx);
void push_swap_rr(t_push_swap_ctx *ctx);
void push_swap_rra(t_push_swap_ctx *ctx);
void push_swap_rrb(t_push_swap_ctx *ctx);
void push_swap_rrr(t_push_swap_ctx *ctx);
void push_swap_sa(t_push_swap_ctx *ctx);
void push_swap_sb(t_push_swap_ctx *ctx);
void push_swap_ss(t_push_swap_ctx *ctx);
#endif
// vi: noet sw=4 ts=4:

48
src/push_swap.c Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include "ft_stack.h"
void push_swap_pa(t_push_swap_ctx *ctx)
{
stack_push(ctx->stack_a, stack_pop(ctx->stack_b));
ctx->instruction_counter++;
}
void push_swap_pb(t_push_swap_ctx *ctx)
{
stack_push(ctx->stack_b, stack_pop(ctx->stack_a));
ctx->instruction_counter++;
}
void push_swap_sa(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_a);
ctx->instruction_counter++;
}
void push_swap_sb(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_b);
ctx->instruction_counter++;
}
void push_swap_ss(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_a);
stack_swap(ctx->stack_b);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

36
src/reverse_rotate.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include "ft_stack.h"
void push_swap_rra(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_a, 1);
ctx->instruction_counter++;
}
void push_swap_rrb(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
void push_swap_rrr(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_a, 1);
stack_rrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

36
src/rotate.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include "ft_stack.h"
void push_swap_ra(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
ctx->instruction_counter++;
}
void push_swap_rb(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
void push_swap_rr(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
stack_lrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

View File

@ -32,7 +32,7 @@ void *stack_pop(t_stack *s)
void *stack_push(t_stack *s, void *item)
{
if (s->top >= s->maxsize)
if (s->top >= s->maxsize || !item)
return (NULL);
ft_memmove(&((char *)(s->items))[s->top], item, s->itemsize);
s->top += s->itemsize;

View File

@ -49,6 +49,27 @@ t_stack *stack_lrotate(t_stack *s, unsigned int step)
return (s);
}
t_stack *stack_swap(t_stack *s)
{
unsigned char byte;
size_t i;
unsigned char *pos;
if (stack_height(s) < 2)
return (NULL);
i = 0;
pos = &((unsigned char *)(s->items))[s->top - s->itemsize];
while (i < s->itemsize)
{
byte = *pos;
*pos = *(pos - s->itemsize);
*(pos - s->itemsize) = byte;
pos++;
i++;
}
return (s);
}
static void *reverse_array(void *start, void *end, size_t itemsize)
{
void *const tmp = malloc(itemsize);
@ -66,6 +87,7 @@ static void *reverse_array(void *start, void *end, size_t itemsize)
end = (char *)end - itemsize;
}
}
free(tmp);
return (start);
}

View File

@ -8,16 +8,16 @@ static void print_stack(t_stack *s);
int main(void)
{
t_stack *s = stack_new(8, sizeof (int));
t_stack *s = stack_new(8, sizeof (long));
for (int item = 0; item < 10; item++)
for (long item = 0; item < 10; item++)
{
printf("push %d\n", item);
printf("push %ld\n", item);
stack_push(s, &item);
print_stack(s);
}
printf("pop %d\n", *(int*)stack_pop(s));
printf("pop %ld\n", *(long int*)stack_pop(s));
print_stack(s);
printf("right rotate step 1\n");
@ -29,6 +29,9 @@ int main(void)
printf("left rotate step 3\n");
stack_lrotate(s, 3);
print_stack(s);
printf("swap\n");
stack_swap(s);
print_stack(s);
return 0;
}
@ -36,6 +39,6 @@ int main(void)
static void print_stack(t_stack *s)
{
for (size_t i = 0; i < stack_height(s); i++)
printf("%d ", ((int*)(s->items))[i]);
printf("%ld ", ((long int*)(s->items))[i]);
putchar('\n');
}

BIN
test

Binary file not shown.