-
Notifications
You must be signed in to change notification settings - Fork 0
Utils
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 usingft_stackadd_back()
.
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 |
create_node(&stack_a, "42"); // Creates a node with the value 42 and adds it to the end of stack A
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 isNULL
or empty, the program is terminated after freeing the array.
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. |
char *split[] = {"3", "5", "8", NULL};
t_stack *stack_a = args_splited(split); // Converts the split array into a stack
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 (whenac == 2
), it splits the argument usingft_split()
and processes it withargs_splited()
. Otherwise, each argument is processed directly withcreate_node()
.
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. |
t_stack *stack_a = args_to_node(argc, argv); // Converts the command-line arguments into a stack
Creates a new stack node with the given content:
This function allocates memory for a newt_stack
node and initializes it with the provided integer value. The rest of the fields (mov
,mov_orientation
,total_mov
, andindex
) are initialized to 0, and thenext
pointer is set toNULL
.
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. |
t_stack *new_node = ft_stacknew(42); // Creates a new node with the value 42
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.
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. |
int result = ck_doubles(&stack_a); // Checks if there are duplicate values in stack A
if (result == 1) {
ft_error(); // Handle error
}
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).
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. |
int is_sorted = ck_sorted(&stack_a); // Checks if stack A is sorted
if (is_sorted == 1) {
// The stack is sorted
}
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.
Library | None |
---|---|
Signature | void ft_error(void); |
Parameters | None |
Return | None |
ft_error(); // Outputs "Error\n" and exits the program
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 toNULL
.
Library | None |
---|---|
Signature | void free_stack(t_stack **stack); |
Parameters |
stack : A pointer to the head of the stack to be freed. |
Return | None |
free_stack(&stack_a); // Frees all nodes in stack A and resets the pointer
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 toNULL
.
Library | None |
---|---|
Signature | void free_split(char **str); |
Parameters |
str : A pointer to the split string array to be freed. |
Return | None |
free_split(split_array); // Frees the split array and sets the pointer to NULL