main - pin3dev/42_PushSwap GitHub Wiki

pushswap

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.

Original Function:

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.

Usage Example:

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.

Specifics:

  • 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 using ft_error().
  • Verifies if the stack is already sorted with ck_sorted(). If not, it triggers sort_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 ➡️

⚠️ **GitHub.com Fallback** ⚠️