diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/action.c | 0 | ||||
| -rw-r--r-- | src/common/action.h | 19 | ||||
| -rw-r--r-- | src/common/stack.c | 80 | ||||
| -rw-r--r-- | src/common/stack.h | 23 |
4 files changed, 122 insertions, 0 deletions
diff --git a/src/common/action.c b/src/common/action.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/common/action.c diff --git a/src/common/action.h b/src/common/action.h new file mode 100644 index 0000000..d458c49 --- /dev/null +++ b/src/common/action.h @@ -0,0 +1,19 @@ +#ifndef ACTION_H +# define ACTION_H + +# include "stack.h" + +void swap_a(t_stack *a); +void swap_b(t_stack *b); +void swap_both(t_stack *a, t_stack *b); +void push_a(t_stack *a, t_stack *b); +void push_b(t_stack *b, t_stack *a); +void rotate_a(t_stack *a); +void rotate_b(t_stack *b); +void rotate_both(t_stack *a, t_stack *b); +void reverse_rotate_a(t_stack *a); +void reverse_rotate_b(t_stack *b); +void reverse_rotate_both(t_stack *a, t_stack *b); + + +#endif diff --git a/src/common/stack.c b/src/common/stack.c new file mode 100644 index 0000000..cbd0986 --- /dev/null +++ b/src/common/stack.c @@ -0,0 +1,80 @@ +#include "stack.h" + +t_stack *stack_new(int size) +{ + t_stack *stack; + + if ((stack = (t_stack*)malloc(sizeof(t_stack))) == NULL) + return (NULL); + if ((stack->elements = (int*)malloc(sizeof(int) * size)) == NULL) + { + free(stack); + return (NULL); + } + stack->top = 0; + return (stack); +} + +void stack_destroy(t_stack *stack) +{ + if (stack == NULL) + return ; + free(stack->elements); + free(stack); +} + +void stack_push(t_stack *stack, int n) +{ + stack->elements[stack->top] = n; + stack->top++; +} + +void stack_pop(t_stack *stack) +{ + stack->top--; +} + +int stack_peek(t_stack *stack) +{ + return (stack->elements[stack->top]); +} + +void stack_swap(t_stack *stack) +{ + int first; + int second; + + 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; + + tmp = stack_peek(from); + stack_pop(from); + stack_push(to, tmp); +} + +void stack_rotate(t_stack *stack) +{ + int tmp; + + tmp = stack_peek(stack); + ft_memmove(&stack->elements[1], stack->elements, sizeof(int) * stack->top); + stack->elements[0] = tmp; +} + +void stack_reverse_rotate(t_stack *stack) +{ + int tmp; + + tmp = stack->elements[0]; + ft_memmove(stack->elements, &stack->elements[1], sizeof(int) * stack->top); + stack_push(stack, tmp); +} diff --git a/src/common/stack.h b/src/common/stack.h new file mode 100644 index 0000000..a85a333 --- /dev/null +++ b/src/common/stack.h @@ -0,0 +1,23 @@ +#ifndef STACK_H +# define STACK_H + +#include <stdlib.h> +#include "libft.h" + +typedef struct +{ + int *elements; + int top; +} t_stack; + +t_stack *stack_new(int size); +void stack_destroy(t_stack *stack); +void stack_push(t_stack *stack, int n); +void stack_pop(t_stack *stack); +int stack_peek(t_stack *stack); +void stack_swap(t_stack *stack); +void stack_push_to(t_stack *from, t_stack *to); +void stack_rotate(t_stack *stack); +void stack_reverse_rotate(t_stack *stack); + +#endif |
