mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
Compare commits
25 Commits
simple_alg
...
headers
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95577e5f02 | ||
|
|
ca732b1b97 | ||
|
|
2b255b94da | ||
|
|
a78b3f1a44 | ||
|
|
4bb0b71141 | ||
|
|
40ec288032 | ||
|
|
54db222c48 | ||
|
|
ef9f16a142 | ||
|
|
80179a5289 | ||
|
|
508b63d296 | ||
|
|
805d592160 | ||
|
|
f98dce6f61 | ||
|
|
ae4740c479 | ||
|
|
dfdcd68549 | ||
|
|
a44b878625 | ||
|
|
e06a51e034 | ||
|
|
85b83f3cc7 | ||
|
|
96a0a6d8f0 | ||
|
|
e563d663ec | ||
|
|
e2495fa62f | ||
|
|
a7cbd18db6 | ||
|
|
48c13b8ef8 | ||
|
|
5372b944a4 | ||
|
|
b6c1dc73a8 | ||
|
|
977a4a4b1a |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -56,3 +56,9 @@ dkms.conf
|
|||||||
|
|
||||||
# Vim swap files
|
# Vim swap files
|
||||||
*.swp
|
*.swp
|
||||||
|
|
||||||
|
# Executable
|
||||||
|
pushswap
|
||||||
|
|
||||||
|
# File obj
|
||||||
|
obj/
|
||||||
|
|||||||
101
Makefile
Normal file
101
Makefile
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
#============================
|
||||||
|
# ALL FOLDERS
|
||||||
|
#============================
|
||||||
|
|
||||||
|
STACK_UTILS_DIR = stack_utils
|
||||||
|
|
||||||
|
ALGO_UTILS_DIR = algorithms/utils
|
||||||
|
|
||||||
|
INSERT_DIR = algorithms/insertion
|
||||||
|
|
||||||
|
ALGO_DIR = algorithms
|
||||||
|
|
||||||
|
PARS_DIR = parsing
|
||||||
|
|
||||||
|
MEDIUM_DIR = medium
|
||||||
|
|
||||||
|
INCLUDES = headers
|
||||||
|
|
||||||
|
#============================
|
||||||
|
# ALL FILES WITHOUT PATH
|
||||||
|
#============================
|
||||||
|
|
||||||
|
SRC = main.c test_one.c
|
||||||
|
|
||||||
|
INSERTION = insertion.c
|
||||||
|
|
||||||
|
PARSING = ft_atoi.c parsing.c parsing_2.c
|
||||||
|
|
||||||
|
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c
|
||||||
|
|
||||||
|
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c
|
||||||
|
|
||||||
|
ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c
|
||||||
|
|
||||||
|
#============================
|
||||||
|
# ADDING PATH TO THE FILES
|
||||||
|
#============================
|
||||||
|
|
||||||
|
ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \
|
||||||
|
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \
|
||||||
|
$(INSERT_DIR)/$(INSERTION)
|
||||||
|
|
||||||
|
OBJ_DIR = obj
|
||||||
|
|
||||||
|
CC = cc
|
||||||
|
|
||||||
|
CFLAGS = -Wall -Werror -Wextra -I$(INCLUDES)
|
||||||
|
|
||||||
|
NAME = push_swap
|
||||||
|
|
||||||
|
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
|
||||||
|
DEP = $(OBJ:.o=.d)
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||||
|
@echo "===================================="
|
||||||
|
@echo "======= PUSH SWAP COMPILED ========="
|
||||||
|
@echo "===================================="
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(INSERT_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(STACK_UTILS_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(ALGO_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR):
|
||||||
|
@mkdir -p $(OBJ_DIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -rf $(OBJ_DIR)
|
||||||
|
@echo "===================================="
|
||||||
|
@echo "= ALL OBJECT AND DEPENDENCES CLEAN ="
|
||||||
|
@echo "===================================="
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@rm -f $(NAME)
|
||||||
|
@echo "========== EXEC DELETED ============"
|
||||||
|
@echo "===================================="
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
-include $(DEP)
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* bubble.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */
|
|
||||||
/* Updated: 2025/12/09 15:56:56 by dgaillet ### ########lyon.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "push_swap.h"
|
|
||||||
#include "unistd.h"
|
|
||||||
|
|
||||||
void bubble_alg(t_stacks *stacks)
|
|
||||||
{
|
|
||||||
if (check_order(stacks->a))
|
|
||||||
return ;
|
|
||||||
while (stack_a_len(stacks))
|
|
||||||
{
|
|
||||||
if (stacks->a->value > stacks->a->next->value)
|
|
||||||
{
|
|
||||||
sa(stacks);
|
|
||||||
write(1, "sa\n", 3);
|
|
||||||
}
|
|
||||||
pb(stacks);
|
|
||||||
write(1, "pb\n", 3);
|
|
||||||
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
|
|
||||||
}
|
|
||||||
while (stack_b_len(stacks))
|
|
||||||
{
|
|
||||||
if (stacks->b->value < stacks->b->next->value)
|
|
||||||
{
|
|
||||||
sb(stacks);
|
|
||||||
write(1, "sa\n", 3);
|
|
||||||
}
|
|
||||||
pa(stacks);
|
|
||||||
write(1, "pb\n", 3);
|
|
||||||
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
|
|
||||||
}
|
|
||||||
bubble_alg(stacks);
|
|
||||||
}
|
|
||||||
@@ -6,35 +6,12 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
|
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/14 17:31:02 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/12/15 14:45:54 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
static int r_to_lowest(t_stack *stack, int len)
|
|
||||||
{
|
|
||||||
t_stack *lowest;
|
|
||||||
int lowest_i;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 1;
|
|
||||||
lowest_i = 0;
|
|
||||||
lowest = stack;
|
|
||||||
stack = stack->next;
|
|
||||||
while (i < len)
|
|
||||||
{
|
|
||||||
if (stack->value < lowest->value)
|
|
||||||
{
|
|
||||||
lowest_i = i;
|
|
||||||
lowest = stack;
|
|
||||||
}
|
|
||||||
stack = stack->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (lowest_i);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int to_insert(t_stacks *stacks, int sorted)
|
static int to_insert(t_stacks *stacks, int sorted)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -76,6 +53,13 @@ void insertion(t_stacks *stacks, int len)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (len == 3)
|
||||||
|
{
|
||||||
|
sort_three_a(stacks);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (check_order(stacks->a))
|
||||||
|
return ;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < len)
|
while (i < len)
|
||||||
{
|
{
|
||||||
|
|||||||
114
algorithms/medium/medium_algo.c
Normal file
114
algorithms/medium/medium_algo.c
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* medium_algo.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/10 07:14:45 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/06 12:57:05 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static int path_to_end(t_stacks *piles, int max_range, int range, char c)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int i;
|
||||||
|
int first_pass;
|
||||||
|
|
||||||
|
if (c == 'a')
|
||||||
|
tmp = piles->a;
|
||||||
|
else
|
||||||
|
tmp = piles->b;
|
||||||
|
tmp = tmp->previous;
|
||||||
|
i = 0;
|
||||||
|
first_pass = 1;
|
||||||
|
while (first_pass || tmp != piles->b->previous)
|
||||||
|
{
|
||||||
|
first_pass = 0;
|
||||||
|
if (in_range(tmp->value, max_range, range))
|
||||||
|
tmp = piles->b;
|
||||||
|
tmp = tmp->previous;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int path_to_start(t_stacks *piles, int max_range, int range, char c)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int i;
|
||||||
|
int first_pass;
|
||||||
|
|
||||||
|
if (c == 'a')
|
||||||
|
tmp = piles->a;
|
||||||
|
else
|
||||||
|
tmp = piles->b;
|
||||||
|
i = 0;
|
||||||
|
first_pass = 1;
|
||||||
|
while (first_pass || tmp != piles->b)
|
||||||
|
{
|
||||||
|
first_pass = 0;
|
||||||
|
if (in_range(tmp->value, max_range, range))
|
||||||
|
{
|
||||||
|
tmp = piles->b->previous;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int wich_path(t_stacks *piles, int max_range, int range, char c)
|
||||||
|
{
|
||||||
|
int path_start;
|
||||||
|
int path_end;
|
||||||
|
|
||||||
|
path_start = path_to_start(piles, max_range, range, c);
|
||||||
|
path_end = path_to_end(piles, max_range, range, c);
|
||||||
|
if (path_start < path_end)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int stack_len(t_stack *stack)
|
||||||
|
{
|
||||||
|
t_stack *first;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
first = stack;
|
||||||
|
i = 1;
|
||||||
|
while (first->next != stack)
|
||||||
|
{
|
||||||
|
first = first->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
||||||
|
{
|
||||||
|
t_tab *tmp;
|
||||||
|
|
||||||
|
tmp = preset;
|
||||||
|
while (piles->a)
|
||||||
|
pb(piles);
|
||||||
|
while (preset)
|
||||||
|
{
|
||||||
|
push_range_to_b(piles, preset, range);
|
||||||
|
if (preset->next)
|
||||||
|
tmp = preset->next;
|
||||||
|
else
|
||||||
|
tmp = NULL;
|
||||||
|
if (preset)
|
||||||
|
free(preset);
|
||||||
|
preset = tmp;
|
||||||
|
}
|
||||||
|
while (piles->b)
|
||||||
|
pa(piles);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
95
algorithms/medium/sort_utils.c
Normal file
95
algorithms/medium/sort_utils.c
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/05 14:11:49 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/05 14:11:52 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
|
||||||
|
static int number_move_reverse(t_stacks *piles, int value, char start_end)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp = piles->a;
|
||||||
|
if (start_end == 's')
|
||||||
|
{
|
||||||
|
while (tmp->value < value)
|
||||||
|
{
|
||||||
|
tmp = tmp->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmp = tmp->previous;
|
||||||
|
while (tmp->value > value)
|
||||||
|
{
|
||||||
|
tmp = tmp->previous;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sort_path(t_stacks *piles, int value)
|
||||||
|
{
|
||||||
|
int start_path;
|
||||||
|
int end_path;
|
||||||
|
|
||||||
|
start_path = number_move_reverse(piles, value, 's');
|
||||||
|
if (start_path == 0)
|
||||||
|
return (1);
|
||||||
|
end_path = number_move_reverse(piles, value, 'e');
|
||||||
|
if (start_path < end_path)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sort_little_pile(t_stacks *piles)
|
||||||
|
{
|
||||||
|
if (!piles->a)
|
||||||
|
{
|
||||||
|
pa(piles);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (piles->a->previous->value < piles->b->value)
|
||||||
|
{
|
||||||
|
pa(piles);
|
||||||
|
ra(piles);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (sort_path(piles, piles->b->value))
|
||||||
|
sort_from_left(piles);
|
||||||
|
else
|
||||||
|
sort_from_right(piles);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
|
||||||
|
{
|
||||||
|
while (one_preset->nb_in > 0)
|
||||||
|
{
|
||||||
|
if (wich_path(piles, one_preset->max_range, range, 'b'))
|
||||||
|
{
|
||||||
|
while (!in_range(piles->b->value, one_preset->max_range, range))
|
||||||
|
rb(piles);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (!in_range(piles->b->value, one_preset->max_range, range))
|
||||||
|
{
|
||||||
|
rrb(piles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort_little_pile(piles);
|
||||||
|
one_preset->nb_in--;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
algorithms/medium/sort_utils_two.c
Normal file
49
algorithms/medium/sort_utils_two.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort_utils_two.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/06 07:52:36 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/06 07:52:38 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void sort_from_left(t_stacks *piles)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (piles->b->value > piles->a->value)
|
||||||
|
{
|
||||||
|
ra(piles);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
pa(piles);
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
rra(piles);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_from_right(t_stacks *piles)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (piles->b->value < piles->a->previous->value)
|
||||||
|
{
|
||||||
|
rra(piles);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
pa(piles);
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
ra(piles);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
93
algorithms/medium/utils_medium.c
Normal file
93
algorithms/medium/utils_medium.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_medium.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/10 13:58:40 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/12/10 13:58:42 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int get_first_lower(t_stack *first)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int lower;
|
||||||
|
|
||||||
|
tmp = first;
|
||||||
|
lower = tmp->value;
|
||||||
|
while (tmp->next != first)
|
||||||
|
{
|
||||||
|
if (lower > tmp->value)
|
||||||
|
lower = tmp->value;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (lower);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_next_lower(t_stack *first, int old_lower)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int next_lower;
|
||||||
|
|
||||||
|
tmp = first;
|
||||||
|
next_lower = 2147483647;
|
||||||
|
while (tmp->next != first)
|
||||||
|
{
|
||||||
|
if (old_lower < tmp->value && tmp->value <= next_lower)
|
||||||
|
{
|
||||||
|
next_lower = tmp->value;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (next_lower);
|
||||||
|
}
|
||||||
|
|
||||||
|
int calcul_range(int value, int range)
|
||||||
|
{
|
||||||
|
int max_range;
|
||||||
|
|
||||||
|
max_range = 0;
|
||||||
|
if (value < 0)
|
||||||
|
while (max_range > value)
|
||||||
|
max_range -= range;
|
||||||
|
else
|
||||||
|
while (max_range <= value)
|
||||||
|
max_range += range;
|
||||||
|
if (max_range < 0)
|
||||||
|
return (max_range + range - 1);
|
||||||
|
return (max_range - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int in_range(int value, int max_range, int range)
|
||||||
|
{
|
||||||
|
int min_range;
|
||||||
|
|
||||||
|
min_range = max_range - (range - 1);
|
||||||
|
if (value <= max_range && value >= min_range)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_number_in_range(int max_range, t_stack *a, int range)
|
||||||
|
{
|
||||||
|
int nb_in;
|
||||||
|
t_stack *tmp;
|
||||||
|
t_stack *first;
|
||||||
|
|
||||||
|
nb_in = 0;
|
||||||
|
tmp = a;
|
||||||
|
first = tmp;
|
||||||
|
while (tmp->next != first)
|
||||||
|
{
|
||||||
|
if (in_range(tmp->value, max_range, range))
|
||||||
|
nb_in++;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
if (in_range(tmp->value, max_range, range))
|
||||||
|
nb_in++;
|
||||||
|
return (nb_in);
|
||||||
|
}
|
||||||
74
algorithms/medium/utils_medium_two.c
Normal file
74
algorithms/medium/utils_medium_two.c
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_medium_two.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/22 09:36:56 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/12/22 09:38:19 by mteriier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
|
||||||
|
static int get_max_number(t_stack *first)
|
||||||
|
{
|
||||||
|
int max;
|
||||||
|
t_stack *a;
|
||||||
|
|
||||||
|
a = first;
|
||||||
|
max = a->value;
|
||||||
|
while (a->next != first)
|
||||||
|
{
|
||||||
|
if (max < a->value)
|
||||||
|
max = a->value;
|
||||||
|
a = a->next;
|
||||||
|
}
|
||||||
|
return (max);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_min_number(t_stack *first)
|
||||||
|
{
|
||||||
|
int min;
|
||||||
|
t_stack *a;
|
||||||
|
|
||||||
|
a = first;
|
||||||
|
min = a->value;
|
||||||
|
while (a->next != first)
|
||||||
|
{
|
||||||
|
if (min > a->value)
|
||||||
|
min = a->value;
|
||||||
|
a = a->next;
|
||||||
|
}
|
||||||
|
return (min);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int my_sqrt(int nb)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (nb < 1)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (i * i <= nb)
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int range_bucket(t_stack *first)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int diff;
|
||||||
|
int sqrt;
|
||||||
|
|
||||||
|
len = stack_len(first);
|
||||||
|
diff = (get_max_number(first) - get_min_number(first)) ;
|
||||||
|
sqrt = my_sqrt(len);
|
||||||
|
if (diff / sqrt < 2)
|
||||||
|
{
|
||||||
|
return (get_max_number(first));
|
||||||
|
}
|
||||||
|
return ((get_max_number(first) - get_min_number(first)) / my_sqrt(len));
|
||||||
|
}
|
||||||
91
algorithms/medium/utils_struct_tab.c
Normal file
91
algorithms/medium/utils_struct_tab.c
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_struct_tab.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/10 13:36:43 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/12/10 13:36:48 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static t_tab *allocate_tab(int range_max, int nb)
|
||||||
|
{
|
||||||
|
t_tab *tab;
|
||||||
|
|
||||||
|
tab = malloc(sizeof(t_tab));
|
||||||
|
if (!tab)
|
||||||
|
return (NULL);
|
||||||
|
tab->next = NULL;
|
||||||
|
tab->max_range = range_max;
|
||||||
|
tab->nb_in = nb;
|
||||||
|
return (tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_tab *free_tab(t_tab **first)
|
||||||
|
{
|
||||||
|
if (!(*first))
|
||||||
|
return (NULL);
|
||||||
|
if ((*first)->next)
|
||||||
|
free_tab(&(*first)->next);
|
||||||
|
free(*first);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_tab *init_first_tab(t_stack *first, int range)
|
||||||
|
{
|
||||||
|
t_tab *tab;
|
||||||
|
int lower;
|
||||||
|
int range_max;
|
||||||
|
|
||||||
|
lower = get_first_lower(first);
|
||||||
|
range_max = calcul_range(lower, range);
|
||||||
|
tab = allocate_tab(range_max, get_number_in_range(range_max, first, range));
|
||||||
|
if (!tab)
|
||||||
|
return (NULL);
|
||||||
|
return (tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_tab *get_next_tab(t_stack *first, t_tab *tab, int range)
|
||||||
|
{
|
||||||
|
int lower;
|
||||||
|
int range_max;
|
||||||
|
t_tab *next_tab;
|
||||||
|
|
||||||
|
lower = get_next_lower(first, tab->max_range);
|
||||||
|
range_max = calcul_range(lower, range);
|
||||||
|
next_tab = allocate_tab(range_max,
|
||||||
|
get_number_in_range(range_max, first, range));
|
||||||
|
if (!next_tab)
|
||||||
|
return (NULL);
|
||||||
|
return (next_tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_tab *get_tabs(t_stack *first, int range)
|
||||||
|
{
|
||||||
|
t_tab *tmp;
|
||||||
|
t_tab *first_tab;
|
||||||
|
int len_stack;
|
||||||
|
int scan_nb_in_tab;
|
||||||
|
|
||||||
|
len_stack = stack_len(first);
|
||||||
|
first_tab = init_first_tab(first, range);
|
||||||
|
if (!first_tab)
|
||||||
|
return (NULL);
|
||||||
|
scan_nb_in_tab = first_tab->nb_in;
|
||||||
|
tmp = first_tab;
|
||||||
|
while (scan_nb_in_tab < len_stack)
|
||||||
|
{
|
||||||
|
tmp->next = get_next_tab(first, tmp, range);
|
||||||
|
if (!(tmp->next))
|
||||||
|
return (free_tab(&first_tab));
|
||||||
|
tmp = tmp->next;
|
||||||
|
scan_nb_in_tab += tmp->nb_in;
|
||||||
|
}
|
||||||
|
return (first_tab);
|
||||||
|
}
|
||||||
@@ -41,5 +41,3 @@ int is_highest(t_stack *stack, t_stack *node, int len)
|
|||||||
}
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,13 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/11 17:49:56 by dgaillet #+# #+# */
|
/* Created: 2025/12/11 17:49:56 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/11 18:29:05 by dgaillet ### ########lyon.fr */
|
/* Updated: 2025/12/15 14:44:56 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
|
static void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
|
||||||
{
|
{
|
||||||
while (i > 0)
|
while (i > 0)
|
||||||
{
|
{
|
||||||
@@ -23,6 +23,8 @@ void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
|
|||||||
|
|
||||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
||||||
{
|
{
|
||||||
|
if (i == len)
|
||||||
|
return ;
|
||||||
if (i && len / i >= 2)
|
if (i && len / i >= 2)
|
||||||
{
|
{
|
||||||
if (stack == 'a')
|
if (stack == 'a')
|
||||||
@@ -30,7 +32,7 @@ void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
|||||||
else
|
else
|
||||||
iterate_fn(stacks, i, &rb);
|
iterate_fn(stacks, i, &rb);
|
||||||
}
|
}
|
||||||
else
|
else if (i)
|
||||||
{
|
{
|
||||||
if (stack == 'a')
|
if (stack == 'a')
|
||||||
iterate_fn(stacks, len - i, &rra);
|
iterate_fn(stacks, len - i, &rra);
|
||||||
|
|||||||
51
algorithms/utils/pre_sort.c
Normal file
51
algorithms/utils/pre_sort.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pre_sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/15 14:21:20 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/15 14:59:52 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int r_to_lowest(t_stack *stack, int len)
|
||||||
|
{
|
||||||
|
t_stack *lowest;
|
||||||
|
int lowest_i;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
lowest_i = 0;
|
||||||
|
lowest = stack;
|
||||||
|
stack = stack->next;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
if (stack->value < lowest->value)
|
||||||
|
{
|
||||||
|
lowest_i = i;
|
||||||
|
lowest = stack;
|
||||||
|
}
|
||||||
|
stack = stack->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (lowest_i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_three_a(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
if (check_order(stacks->a))
|
||||||
|
return ;
|
||||||
|
if (stacks->a->value > stacks->a->next->value)
|
||||||
|
sa(stacks);
|
||||||
|
optimal_rotate(stacks, r_to_lowest(stacks->a, 3), 3, 'a');
|
||||||
|
if (stacks->a->next->value > stacks->a->previous->value)
|
||||||
|
{
|
||||||
|
ra(stacks);
|
||||||
|
sa(stacks);
|
||||||
|
rra(stacks);
|
||||||
|
}
|
||||||
|
}
|
||||||
41
headers/medium_headers.h
Normal file
41
headers/medium_headers.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* medium_headers.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 07:47:49 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/07 07:47:51 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef MEDIUM_HEADERS_H
|
||||||
|
# define MEDIUM_HEADERS_H
|
||||||
|
|
||||||
|
typedef struct s_tab
|
||||||
|
{
|
||||||
|
int max_range;
|
||||||
|
int nb_in;
|
||||||
|
struct s_tab *next;
|
||||||
|
} t_tab;
|
||||||
|
|
||||||
|
/* MEDIUM ALGO FILE */
|
||||||
|
int wich_path(t_stacks *piles, int max_range, int range, char c);
|
||||||
|
int stack_len(t_stack *stack);
|
||||||
|
void bucket_algo(t_stacks *piles, t_tab *preset, int range);
|
||||||
|
/* SORT UTILS FILES */
|
||||||
|
void sort_from_left(t_stacks *piles);
|
||||||
|
void sort_from_right(t_stacks *piles);
|
||||||
|
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range);
|
||||||
|
/* MEDIUM UTILS FILES */
|
||||||
|
int range_bucket(t_stack *first);
|
||||||
|
int get_first_lower(t_stack *first);
|
||||||
|
int get_next_lower(t_stack *first, int old_lower);
|
||||||
|
int calcul_range(int value, int range);
|
||||||
|
int in_range(int value, int max_range, int range);
|
||||||
|
int get_number_in_range(int max_range, t_stack *a, int range);
|
||||||
|
/* UTILS STRUCT TAB FILE */
|
||||||
|
t_tab *get_tabs(t_stack *first, int range);
|
||||||
|
|
||||||
|
#endif
|
||||||
20
headers/parsing.h
Normal file
20
headers/parsing.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/07 08:03:10 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PARSING_H
|
||||||
|
# define PARSING_H
|
||||||
|
|
||||||
|
int ft_atoi(const char *nptr);
|
||||||
|
t_stacks *init_big_stacks2(int *tab, int len);
|
||||||
|
t_stacks *init_big_stacks(int argc, char **argv);
|
||||||
|
|
||||||
|
#endif
|
||||||
68
headers/push_swap.h
Normal file
68
headers/push_swap.h
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* push_swap.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/12/15 14:34:45 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PUSH_SWAP_H
|
||||||
|
# define PUSH_SWAP_H
|
||||||
|
|
||||||
|
typedef struct s_stack
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
struct s_stack *next;
|
||||||
|
struct s_stack *previous;
|
||||||
|
} t_stack;
|
||||||
|
|
||||||
|
typedef struct s_stacks
|
||||||
|
{
|
||||||
|
t_stack *a;
|
||||||
|
t_stack *b;
|
||||||
|
} t_stacks;
|
||||||
|
|
||||||
|
/* PRINT STACK FUNCTION*/
|
||||||
|
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
|
||||||
|
/*STACK_FUNCTIONS*/
|
||||||
|
void pa(t_stacks *stacks);
|
||||||
|
void pb(t_stacks *stacks);
|
||||||
|
void rra(t_stacks *stacks);
|
||||||
|
void rrb(t_stacks *stacks);
|
||||||
|
void rrr(t_stacks *stacks);
|
||||||
|
void ra(t_stacks *stacks);
|
||||||
|
void rb(t_stacks *stacks);
|
||||||
|
void rr(t_stacks *stacks);
|
||||||
|
void sa(t_stacks *stacks);
|
||||||
|
void sb(t_stacks *stacks);
|
||||||
|
void ss(t_stacks *stacks);
|
||||||
|
/* STACK ADD AND CLEAR FILES */
|
||||||
|
t_stack *new_stack(int value);
|
||||||
|
void stack_add_back(t_stack **stack, t_stack *new);
|
||||||
|
void stack_add_front(t_stack **stack, t_stack *new);
|
||||||
|
void stack_clear_all(t_stack *stack, t_stack *first);
|
||||||
|
/* STACKS LEN FILES */
|
||||||
|
int stack_a_len(t_stacks *stacks);
|
||||||
|
int stack_b_len(t_stacks *stacks);
|
||||||
|
int highest_stack_len(t_stacks *stacks);
|
||||||
|
/* PRE SORT */
|
||||||
|
int r_to_lowest(t_stack *stack, int len);
|
||||||
|
void sort_three_a(t_stacks *stacks);
|
||||||
|
/* ITERATE FILE */
|
||||||
|
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
|
||||||
|
/* COMPARE FILE */
|
||||||
|
int is_lowest(t_stack *stack, t_stack *node, int len);
|
||||||
|
int is_highest(t_stack *stack, t_stack *node, int len);
|
||||||
|
/* CHECK ORDER FILE */
|
||||||
|
int check_order(t_stack *stack);
|
||||||
|
/* INSERTION */
|
||||||
|
void insertion(t_stacks *stacks, int len);
|
||||||
|
int test2(char **argv);
|
||||||
|
/* TEST FILE */
|
||||||
|
int test1(int argc, char **argv);
|
||||||
|
|
||||||
|
#endif
|
||||||
59
main.c
59
main.c
@@ -11,65 +11,10 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
void print_all_stack(t_stack *stack, t_stack *first, char pile)
|
|
||||||
{
|
|
||||||
t_stack *tmp;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
tmp = stack;
|
|
||||||
i = 0;
|
|
||||||
printf("TAB %c : \n", pile);
|
|
||||||
if (!stack || !first)
|
|
||||||
return ;
|
|
||||||
while (tmp->next != first)
|
|
||||||
{
|
|
||||||
printf("[%d] ", tmp->value);
|
|
||||||
tmp = tmp->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
printf("[%d] \n", tmp->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
t_stacks *stacks;
|
|
||||||
|
|
||||||
stacks = NULL;
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
{
|
test1(argc, argv);
|
||||||
stacks = init_big_stacks(argc, argv);
|
return (0);
|
||||||
/* print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
sa(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
pb(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
pa(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
rra(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
pb(stacks);
|
|
||||||
pb(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
rrb(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
rrr(stacks);
|
|
||||||
print_all_stack(stacks->a, stacks->a, 'A');
|
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
|
||||||
*/
|
|
||||||
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
|
|
||||||
insertion(stacks, stack_a_len(stacks));
|
|
||||||
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
|
|
||||||
}
|
|
||||||
stack_clear_all(stacks->a, stacks->a);
|
|
||||||
stack_clear_all(stacks->b, stacks->b);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,10 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_stack *parsing(int argc, char **argv)
|
static t_stack *parsing(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int stock;
|
int stock;
|
||||||
@@ -45,6 +46,8 @@ t_stacks *init_big_stacks(int argc, char **argv)
|
|||||||
t_stack *a;
|
t_stack *a;
|
||||||
|
|
||||||
stacks = malloc(sizeof(t_stacks));
|
stacks = malloc(sizeof(t_stacks));
|
||||||
|
stacks->a = NULL;
|
||||||
|
stacks->b = NULL;
|
||||||
if (!stacks)
|
if (!stacks)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
a = parsing(argc, argv);
|
a = parsing(argc, argv);
|
||||||
55
parsing/parsing_2.c
Normal file
55
parsing/parsing_2.c
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing_2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/22 13:10:58 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/12/22 13:11:21 by mteriier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static t_stack *parsing2(int *tab, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int stock;
|
||||||
|
t_stack *first;
|
||||||
|
t_stack *new;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
first = NULL;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
stock = tab[i];
|
||||||
|
new = new_stack(stock);
|
||||||
|
if (!new && !first)
|
||||||
|
return (NULL);
|
||||||
|
else if (!new)
|
||||||
|
{
|
||||||
|
stack_clear_all(first, first);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
stack_add_back(&first, new);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (first);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stacks *init_big_stacks2(int *tab, int len)
|
||||||
|
{
|
||||||
|
t_stacks *stacks;
|
||||||
|
t_stack *a;
|
||||||
|
|
||||||
|
stacks = malloc(sizeof(t_stacks));
|
||||||
|
stacks->a = NULL;
|
||||||
|
stacks->b = NULL;
|
||||||
|
if (!stacks)
|
||||||
|
return (NULL);
|
||||||
|
a = parsing2(tab, len);
|
||||||
|
stacks->a = a;
|
||||||
|
return (stacks);
|
||||||
|
}
|
||||||
67
push_swap.h
67
push_swap.h
@@ -1,67 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* push_swap.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
|
||||||
/* Updated: 2025/12/14 16:55:24 by dgaillet ### ########lyon.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef PUSH_SWAP_H
|
|
||||||
# define PUSH_SWAP_H
|
|
||||||
|
|
||||||
typedef struct s_stack
|
|
||||||
{
|
|
||||||
int value;
|
|
||||||
struct s_stack *next;
|
|
||||||
struct s_stack *previous;
|
|
||||||
} t_stack;
|
|
||||||
|
|
||||||
typedef struct s_stacks
|
|
||||||
{
|
|
||||||
t_stack *a;
|
|
||||||
t_stack *b;
|
|
||||||
} t_stacks;
|
|
||||||
|
|
||||||
/*STACK_FUNCTIONS*/
|
|
||||||
void pa(t_stacks *stacks);
|
|
||||||
void pb(t_stacks *stacks);
|
|
||||||
void rra(t_stacks *stacks);
|
|
||||||
void rrb(t_stacks *stacks);
|
|
||||||
void rrr(t_stacks *stacks);
|
|
||||||
void ra(t_stacks *stacks);
|
|
||||||
void rb(t_stacks *stacks);
|
|
||||||
void rr(t_stacks *stacks);
|
|
||||||
void sa(t_stacks *stacks);
|
|
||||||
void sb(t_stacks *stacks);
|
|
||||||
void ss(t_stacks *stacks);
|
|
||||||
|
|
||||||
/*FUNCTION UTILS*/
|
|
||||||
t_stack *new_stack(int value);
|
|
||||||
void stack_add_back(t_stack **stack, t_stack *new);
|
|
||||||
void stack_add_front(t_stack **stack, t_stack *new);
|
|
||||||
void stack_clear_all(t_stack *stack, t_stack *first);
|
|
||||||
t_stack *parsing(int argc, char **argv);
|
|
||||||
t_stacks *init_big_stacks(int argc, char **argv);
|
|
||||||
int ft_atoi(const char *nptr);
|
|
||||||
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
|
|
||||||
int stack_a_len(t_stacks *stacks);
|
|
||||||
int stack_b_len(t_stacks *stacks);
|
|
||||||
int highest_stack_len(t_stacks *stacks);
|
|
||||||
|
|
||||||
/*ALGORITHM UTILS*/
|
|
||||||
int check_order(t_stack *stack);
|
|
||||||
void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks));
|
|
||||||
int is_lowest(t_stack *stack, t_stack *node, int len);
|
|
||||||
int is_highest(t_stack *stack, t_stack *node, int len);
|
|
||||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
|
|
||||||
|
|
||||||
/*ALGORITHMS*/
|
|
||||||
void bubble_alg(t_stacks *stacks);
|
|
||||||
//void insertion(t_stacks *stacks, int a_len, int b_len);
|
|
||||||
void insertion(t_stacks *stacks, int len);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -33,8 +33,6 @@ void pa(t_stacks *stacks)
|
|||||||
write(1, "pa\n", 3);
|
write(1, "pa\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void pb(t_stacks *stacks)
|
void pb(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
t_stack *a_push;
|
t_stack *a_push;
|
||||||
|
|||||||
37
test_one.c
Normal file
37
test_one.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* test_one.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/12/22 12:34:35 by mteriier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int test1(int argc, char **argv)
|
||||||
|
{
|
||||||
|
t_stacks *piles;
|
||||||
|
t_tab *preset;
|
||||||
|
|
||||||
|
piles = NULL;
|
||||||
|
if (argc > 1)
|
||||||
|
{
|
||||||
|
piles = init_big_stacks(argc, argv);
|
||||||
|
preset = get_tabs(piles->a, range_bucket(piles->a));
|
||||||
|
bucket_algo(piles, preset, range_bucket(piles->a));
|
||||||
|
}
|
||||||
|
if (piles->a)
|
||||||
|
stack_clear_all(piles->a, piles->a);
|
||||||
|
if (piles->b)
|
||||||
|
stack_clear_all(piles->b, piles->b);
|
||||||
|
free(piles);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user