Skip to content
Ivany Pinheiro edited this page Oct 8, 2024 · 2 revisions

create_node

Creates a new node and adds it to the end of the stack:
This function converts the provided string (str) into an integer using _atoi(), then creates a new node with the integer value. The new node is added to the end of the stack using ft_stackadd_back().

Original Function:

Library None
Signature void create_node(t_stack **head, char *str);
Parameters head: A pointer to the head of the stack.
str: The string to be converted into an integer and used as the content of the new node.
Return None

Usage Example:

create_node(&stack_a, "42");  // Creates a node with the value 42 and adds it to the end of stack A

args_splited

Converts a split string array into a stack of nodes:
This function processes an array of split strings, converts each string into an integer, and creates a corresponding node in a stack. If the split array is NULL or empty, the program is terminated after freeing the array.

Original Function:

Library None
Signature t_stack *args_splited(char **split);
Parameters split: A split string array to be converted into a stack of nodes.
Return A pointer to the head of the newly created stack.

Usage Example:

char *split[] = {"3", "5", "8", NULL};
t_stack *stack_a = args_splited(split);  // Converts the split array into a stack

args_to_node

Converts command-line arguments into a stack of nodes:
This function converts command-line arguments into a linked list of stack nodes. If only one argument is passed (when ac == 2), it splits the argument using ft_split() and processes it with args_splited(). Otherwise, each argument is processed directly with create_node().

Original Function:

Library None
Signature t_stack *args_to_node(int ac, char **av);
Parameters ac: The argument count from the command-line input.
av: The argument vector containing the values to be converted into nodes.
Return A pointer to the head of the newly created stack.

Usage Example:

t_stack *stack_a = args_to_node(argc, argv);  // Converts the command-line arguments into a stack

ft_stacknew

Creates a new stack node with the given content:
This function allocates memory for a new t_stack node and initializes it with the provided integer value. The rest of the fields (mov, mov_orientation, total_mov, and index) are initialized to 0, and the next pointer is set to NULL.

Original Function:

Library None
Signature t_stack *ft_stacknew(int content);
Parameters content: The integer value to be stored in the new node.
Return A pointer to the newly created node, or NULL if memory allocation fails.

Usage Example:

t_stack *new_node = ft_stacknew(42);  // Creates a new node with the value 42

ck_doubles

Checks for duplicate values in the stack:
This function checks the stack for duplicate values by comparing the content of each node with every other node. If duplicates are found, it returns 1; otherwise, it returns 0.

Original Function:

Library None
Signature int ck_doubles(t_stack **stack_a);
Parameters stack_a: A pointer to the stack that will be checked for duplicate values.
Return 1 if duplicates are found, and 0 if no duplicates are found.

Usage Example:

int result = ck_doubles(&stack_a);  // Checks if there are duplicate values in stack A
if (result == 1) {
    ft_error();  // Handle error
}

ck_sorted

Checks if the stack is sorted in ascending order:
This function verifies if the stack is sorted by iterating through its nodes and comparing values. If any node's value is greater than the next node's value, it returns 0 (not sorted); otherwise, it returns 1 (sorted).

Original Function:

Library None
Signature int ck_sorted(t_stack **stack);
Parameters stack: A pointer to the stack that will be checked for sorting.
Return 1 if the stack is sorted in ascending order, and 0 if not sorted.

Usage Example:

int is_sorted = ck_sorted(&stack_a);  // Checks if stack A is sorted
if (is_sorted == 1) {
    // The stack is sorted
}

ft_error

Outputs an error message and terminates the program:
This function prints the message "Error\n" to the standard error output and terminates the program with an exit status of 1.

Original Function:

Library None
Signature void ft_error(void);
Parameters None
Return None

Usage Example:

ft_error();  // Outputs "Error\n" and exits the program

free_stack

Frees all nodes in the stack and resets the pointer to NULL:
This function frees the memory allocated for each node in the stack and resets the stack pointer to NULL.

Original Function:

Library None
Signature void free_stack(t_stack **stack);
Parameters stack: A pointer to the head of the stack to be freed.
Return None

Usage Example:

free_stack(&stack_a);  // Frees all nodes in stack A and resets the pointer

free_split

Frees a split string array and sets its pointer to NULL:
This function frees the memory allocated for each string in a split string array and then sets the pointer to NULL.

Original Function:

Library None
Signature void free_split(char **str);
Parameters str: A pointer to the split string array to be freed.
Return None

Usage Example:

free_split(split_array);  // Frees the split array and sets the pointer to NULL

Previous ⬅️ Top ⬆️