Push Swap
This commit is contained in:
81
main.c
Normal file
81
main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/13 05:46:55 by aortigos #+# #+# */
|
||||
/* Updated: 2024/08/16 06:16:45 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int is_sorted(t_stack *stack)
|
||||
{
|
||||
while (stack->next != NULL)
|
||||
{
|
||||
if (stack->value > stack->next->value)
|
||||
return (0);
|
||||
stack = stack->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static void push_swap(t_stack **stack_a, t_stack **stack_b, int stack_size)
|
||||
{
|
||||
if (stack_size == 2 && !is_sorted(*stack_a))
|
||||
ft_sa(stack_a);
|
||||
else if (stack_size == 3 && !is_sorted(*stack_a))
|
||||
three_sort(stack_a);
|
||||
else if (stack_size > 3 && !is_sorted(*stack_a))
|
||||
sort(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void get_numbers(char *argv, t_stack **stack_a)
|
||||
{
|
||||
char **param;
|
||||
long int n;
|
||||
int i;
|
||||
|
||||
param = ft_split(argv, ' ');
|
||||
i = 0;
|
||||
while (param[i] != NULL)
|
||||
{
|
||||
if (input_is_correct(param[i]))
|
||||
{
|
||||
n = ft_atoi(param[i]);
|
||||
if (n > INT_MAX || n < INT_MIN)
|
||||
error_exit(stack_a, NULL);
|
||||
stack_add(stack_a, stack_new(n));
|
||||
}
|
||||
else
|
||||
error_exit(NULL, NULL);
|
||||
free(param[i]);
|
||||
i++;
|
||||
}
|
||||
free(param);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
int stack_size;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
stack_a = NULL;
|
||||
stack_b = NULL;
|
||||
while (i < argc)
|
||||
get_numbers(argv[i++], &stack_a);
|
||||
if (is_duplicated(stack_a))
|
||||
error_exit(&stack_a, NULL);
|
||||
stack_size = get_stack_size(stack_a);
|
||||
get_index(stack_a, stack_size + 1);
|
||||
push_swap(&stack_a, &stack_b, stack_size);
|
||||
free_stack(&stack_a);
|
||||
free_stack(&stack_b);
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user