-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement swap stack * implement rotate stack * implement reverse rotate stack * refactor with sort() * implement stack operation on nano_sort() * fix norm with src/stack_operations/operator.c * refactor _handle_3 * implement push stack * refacotr test * implement micro sort case 4 * refactor stack operation * modify sort argument * implement sort test * refactor micro sort * implement micro sort case 5 * fix if statement on _handle_4
- Loading branch information
Showing
17 changed files
with
729 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 13:19:05 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/17 16:22:03 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -27,7 +27,21 @@ int check_args(int argc, char **argv); | |
|
||
t_list *generate_stack(int argc, char **argv); | ||
|
||
void nano_sort(t_list *stack, int argc); | ||
void sort(t_list **stack_a, t_list **stack_b, int num_a); | ||
void nano_sort(t_list **stack, int num_a); | ||
void micro_sort(t_list **stack_a, t_list **stack_b, int num_a); | ||
|
||
void operate_sa(t_list **stack_a); | ||
void operate_sb(t_list **stack_b); | ||
void operate_ra(t_list **stack_a); | ||
void operate_rra(t_list **stack_a); | ||
void operate_pa(t_list **stack_b, t_list **stack_a); | ||
void operate_pb(t_list **stack_a, t_list **stack_b); | ||
void push_stack(t_list **stack_1, t_list **stack_2); | ||
void swap_stack(t_list **stack); | ||
void rotate_stack(t_list **stack); | ||
void reverse_rotate_stack(t_list **stack); | ||
t_list *ft_lst_before(t_list *lst, t_list *trg); | ||
|
||
void exit_with_error(void); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 13:04:27 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/16 13:13:30 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -26,19 +26,20 @@ void destructor(void) | |
|
||
void put_void(void *content) | ||
{ | ||
puts(content); | ||
ft_printf("%d\n", *(int *)content); | ||
} | ||
|
||
#endif | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
t_list *stack_a; | ||
t_list *stack_b; | ||
|
||
check_args(argc, argv); | ||
stack_a = generate_stack(argc, argv); | ||
if (argc < 5) | ||
nano_sort(stack_a, argc); | ||
stack_b = NULL; | ||
sort(&stack_a, &stack_b, argc - 1); | ||
ft_lstclear(&stack_a, free); | ||
return (0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* micro_sort.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/17 17:43:30 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
static int _first(t_list **stack) | ||
{ | ||
return (*(int *)(*stack)->content); | ||
} | ||
|
||
static int _second(t_list **stack) | ||
{ | ||
return (*(int *)(*stack)->next->content); | ||
} | ||
|
||
static void _handle_4(t_list **stack_a, t_list **stack_b) | ||
{ | ||
operate_pb(stack_a, stack_b); | ||
nano_sort(stack_a, 3); | ||
if (_first(stack_b) == 1) | ||
operate_pa(stack_b, stack_a); | ||
else if (_first(stack_b) == 2) | ||
{ | ||
operate_pa(stack_b, stack_a); | ||
operate_sa(stack_a); | ||
} | ||
else if (_first(stack_b) == 3) | ||
{ | ||
operate_rra(stack_a); | ||
operate_pa(stack_b, stack_a); | ||
operate_ra(stack_a); | ||
operate_ra(stack_a); | ||
} | ||
else if (_first(stack_b) == 4) | ||
{ | ||
operate_pa(stack_b, stack_a); | ||
operate_ra(stack_a); | ||
} | ||
} | ||
|
||
static void _handle_5(t_list **stack_a, t_list **stack_b) | ||
{ | ||
operate_pb(stack_a, stack_b); | ||
operate_pb(stack_a, stack_b); | ||
nano_sort(stack_a, 3); | ||
if (_first(stack_b) < _second(stack_b)) | ||
operate_sb(stack_b); | ||
if (_first(stack_b) == 5) | ||
{ | ||
operate_pa(stack_b, stack_a); | ||
operate_rra(stack_a); | ||
} | ||
else | ||
{ | ||
while (_first(stack_a) != _first(stack_b) + 1) | ||
operate_ra(stack_a); | ||
operate_pa(stack_b, stack_a); | ||
} | ||
while (_first(stack_a) != _first(stack_b) + 1) | ||
operate_rra(stack_a); | ||
operate_pa(stack_b, stack_a); | ||
while (_first(stack_a) != 1) | ||
operate_rra(stack_a); | ||
} | ||
|
||
void micro_sort(t_list **stack_a, t_list **stack_b, int num_a) | ||
{ | ||
if (num_a == 4) | ||
_handle_4(stack_a, stack_b); | ||
else if (num_a == 5) | ||
_handle_5(stack_a, stack_b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,57 +6,57 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 12:58:12 by reasuke ### ########.fr */ | ||
/* Updated: 2024/01/17 16:04:38 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
static void _handle_2(t_list *stack) | ||
static void _handle_2(t_list **stack) | ||
{ | ||
int first; | ||
int second; | ||
|
||
first = *(int *)stack->content; | ||
second = *(int *)stack->next->content; | ||
first = *(int *)(*stack)->content; | ||
second = *(int *)(*stack)->next->content; | ||
if (first > second) | ||
{ | ||
swap_stack(stack); | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
} | ||
} | ||
|
||
static void _handel_3(t_list *stack) | ||
static void _handle_3(t_list **stack) | ||
{ | ||
int first; | ||
int second; | ||
int third; | ||
|
||
first = *(int *)stack->content; | ||
second = *(int *)stack->next->content; | ||
third = *(int *)stack->next->next->content; | ||
if (first == 2 && second == 1 && third == 3) | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
else if (first == 3 && second == 2 && third == 1) | ||
first = *(int *)(*stack)->content; | ||
second = *(int *)(*stack)->next->content; | ||
third = *(int *)(*stack)->next->next->content; | ||
if (second < first && first < third) | ||
operate_sa(stack); | ||
else if (third < second && second < first) | ||
{ | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
operate_sa(stack); | ||
operate_rra(stack); | ||
} | ||
else if (first == 3 && second == 1 && third == 2) | ||
ft_putendl_fd("ra", STDOUT_FILENO); | ||
else if (first == 1 && second == 3 && third == 2) | ||
else if (second < third && third < first) | ||
operate_ra(stack); | ||
else if (first < third && third < second) | ||
{ | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
ft_putendl_fd("ra", STDOUT_FILENO); | ||
operate_sa(stack); | ||
operate_ra(stack); | ||
} | ||
else if (first == 2 && second == 3 && third == 1) | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
else if (third < first && first < second) | ||
operate_rra(stack); | ||
} | ||
|
||
void nano_sort(t_list *stack, int argc) | ||
void nano_sort(t_list **stack, int num_a) | ||
{ | ||
int num_of_element; | ||
|
||
num_of_element = argc - 1; | ||
if (num_of_element == 2) | ||
if (num_a == 2) | ||
_handle_2(stack); | ||
else if (num_of_element == 3) | ||
_handel_3(stack); | ||
else if (num_a == 3) | ||
_handle_3(stack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* sort.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/15 15:10:35 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/17 16:08:05 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
void sort(t_list **stack_a, t_list **stack_b, int num_a) | ||
{ | ||
if (num_a <= 3) | ||
nano_sort(stack_a, num_a); | ||
else if (num_a <= 5) | ||
micro_sort(stack_a, stack_b, num_a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_lst_before.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/15 16:54:29 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 16:54:44 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
t_list *ft_lst_before(t_list *lst, t_list *trg) | ||
{ | ||
t_list *before; | ||
|
||
if (!lst) | ||
return (NULL); | ||
before = lst; | ||
while (before->next && before->next != trg) | ||
before = before->next; | ||
if (before->next != trg) | ||
return (NULL); | ||
return (before); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* push.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/15 15:56:53 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 17:02:11 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
void push_stack(t_list **stack_1, t_list **stack_2) | ||
{ | ||
t_list *stack_1_first; | ||
|
||
if (!*stack_1) | ||
return ; | ||
stack_1_first = *stack_1; | ||
*stack_1 = (*stack_1)->next; | ||
stack_1_first->next = NULL; | ||
ft_lstadd_front(stack_2, stack_1_first); | ||
} | ||
|
||
void operate_pa(t_list **stack_b, t_list **stack_a) | ||
{ | ||
push_stack(stack_b, stack_a); | ||
ft_putendl_fd("pa", STDOUT_FILENO); | ||
} | ||
|
||
void operate_pb(t_list **stack_a, t_list **stack_b) | ||
{ | ||
push_stack(stack_a, stack_b); | ||
ft_putendl_fd("pb", STDOUT_FILENO); | ||
} |
Oops, something went wrong.