add moves and split utils frommain file

This commit is contained in:
Willem Haffmans 2024-11-06 20:14:21 +00:00
parent 563ebe60a6
commit 5cad71fcd3
9 changed files with 197 additions and 64 deletions

View File

@ -6,7 +6,7 @@
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ # # By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ #
# +#+ +#+#+ +#++#+ +#+ \o/ # # +#+ +#+#+ +#++#+ +#+ \o/ #
# Created: 2024/10/15 11:48:46 by whaffman #+#+# #+#+# #+# #+# | # # Created: 2024/10/15 11:48:46 by whaffman #+#+# #+#+# #+# #+# | #
# Updated: 2024/11/04 13:56:19 by whaffman ### ### ### ### / \ # # Updated: 2024/11/06 20:10:28 by whaffman ### ### ### ### / \ #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -18,9 +18,10 @@ LIBFT_INC_PATH = libft/inc
LIBFT = libft/libft.a LIBFT = libft/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = $(SRC_PATH) VPATH = src:src/util:src/moves
SOURCES = push_swap.c SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \
swap.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -63,7 +64,7 @@ re: fclean all
run: all run: all
@$(eval ARG = $(shell shuf -i 0-100 -n 5)) @$(eval ARG = $(shell shuf -i 0-100 -n 5))
./$(NAME) $(ARG) ./$(NAME) 1 2 3 4 5 6 7 8 9
debug: CFLAGS += -DDEBUG -g debug: CFLAGS += -DDEBUG -g
debug: all debug: all

View File

@ -6,28 +6,23 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/04 14:11:03 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/06 20:10:14 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
typedef struct s_clist #ifndef PUSH_SWAP_H
{ # define PUSH_SWAP_H
void *content;
struct s_clist *next;
struct s_clist *prev;
} t_clist;
typedef struct s_node # include "libft.h"
{
int n; int new_element(t_list **stack, const char *str);
int ra; t_list *ft_lstat(t_list *list, int n);
int rb; void print_stack(const char *name, t_list *stack);
int rr; void print_stacks(t_list *stack_a, t_list *stack_b);
int rra;
int rrb; void push(t_list **stack_1, t_list **stack_2);
int rrr; void rotate(t_list **stack);
int pa; void reverse_rotate(t_list **stack);
int pb; void swap(t_list **stack);
int sa;
int sb; #endif
} t_node;

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* push.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 19:27:09 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 19:38:01 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
void push(t_list **stack_1, t_list **stack_2)
{
t_list *temp;
if (!stack_1)
return ;
temp = (*stack_1)->next;
ft_lstadd_front(stack_2, *stack_1);
*stack_1 = temp;
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* reverse_rotate.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 19:58:58 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 20:03:30 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void reverse_rotate(t_list **stack)
{
t_list *temp;
t_list *last;
if (!stack)
return ;
temp = *stack;
last = ft_lstlast(*stack);
while (temp->next->next)
temp = temp->next;
temp->next = NULL;
ft_lstadd_front(stack, last);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 19:44:17 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 19:51:04 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void rotate(t_list **stack)
{
t_list *temp;
if (!stack)
return ;
temp = (*stack)->next;
(*stack)->next = NULL;
ft_lstadd_back(&temp, *stack);
*stack = temp;
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* swap.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 20:07:09 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 20:10:41 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void swap(t_list **stack)
{
int *temp;
temp = (*stack)->next->content;
(*stack)->next->content = (*stack)->content;
(*stack)->content = temp;
}

View File

@ -6,52 +6,15 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/04 13:04:42 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/06 20:11:40 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
#include "push_swap.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int new_element(t_list **stack, const char *str)
{
int *n;
char *test;
t_list *elem;
n = malloc(sizeof (int));
if (!n)
return (0);
*n = ft_atoi(str);
test = ft_itoa(*n);
if (ft_strncmp(str, test, 20))
{
free(test);
return (0);
}
free(test);
elem = ft_lstnew(n);
if (!elem)
return (0);
ft_lstadd_front(stack, elem);
return (1);
}
t_list *ft_lstat(t_list *list, int n)
{
if (!list)
return (NULL);
while (list->next && n > 0)
{
list = list->next;
n--;
}
if (n > 0)
return (NULL);
return (list);
}
void print_stack(const char *name, t_list *stack) void print_stack(const char *name, t_list *stack)
{ {
int lst_size; int lst_size;
@ -94,6 +57,13 @@ int main(int argc, char *argv[])
return (1); return (1);
} }
} }
push(&stack_a, &stack_b);
push(&stack_a, &stack_b);
push(&stack_a, &stack_b);
rotate(&stack_a);
reverse_rotate(&stack_b);
print_stacks(stack_a, stack_b);
swap(&stack_a);
print_stacks(stack_a, stack_b); print_stacks(stack_a, stack_b);
return (0); return (0);
} }

28
src/util/ft_lstat.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_lstat.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 19:20:38 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 19:24:52 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
t_list *ft_lstat(t_list *list, int n)
{
if (!list)
return (NULL);
while (list->next && n > 0)
{
list = list->next;
n--;
}
if (n > 0)
return (NULL);
return (list);
}

39
src/util/new_element.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* new_element.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/06 19:19:32 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/06 19:37:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
#include <stdlib.h>
int new_element(t_list **stack, const char *str)
{
int *n;
char *test;
t_list *elem;
n = malloc(sizeof (int));
if (!n)
return (0);
*n = ft_atoi(str);
test = ft_itoa(*n);
if (ft_strncmp(str, test, 20))
{
free(test);
return (0);
}
free(test);
elem = ft_lstnew(n);
if (!elem)
return (0);
ft_lstadd_front(stack, elem);
return (1);
}