main - pin3dev/42_PushSwap GitHub Wiki
Entry point for the
push_swap
program:
This function initializes the sorting process, validating input and checking if the stack needs to be sorted.
If necessary, it applies the appropriate sorting algorithm and frees allocated memory before exiting.
Library |
<stdlib.h> , libft.h
|
---|---|
Signature | int pushswap(int ac, char **av); |
Parameters |
ac : The number of arguments passed to the program. |
av : An array of strings containing the arguments, which are the values to be sorted. |
|
Return | Returns 0 on successful execution. |
int ac = 4;
char *av[] = {"./push_swap", "3", "2", "1"};
int result = pushswap(ac, av); // Executes the sorting process, checking for duplicates and sorting the stack.
- Converts the command-line arguments into a linked list of stack nodes using
args_to_node()
.- Checks for duplicate values with
ck_doubles()
. If duplicates are found, it frees the memory and exits with an error usingft_error()
.- Verifies if the stack is already sorted with
ck_sorted()
. If not, it triggerssort_cases()
to sort the stack based on its size.- Handles memory cleanup with
free_stack()
whether the sorting succeeds or fails due to errors.- The function ensures robust error handling through
ft_error()
to output appropriate messages when an issue occurs during execution.
Previous ⬅️ • Top ⬆️ • Next ➡️