Skip to content

Commit

Permalink
add index in nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
vflorez committed Dec 19, 2023
1 parent 5eae90a commit 1d0785d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: vflorez <vflorez@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/10/02 17:28:00 by vflorez #+# #+# #
# Updated: 2023/12/18 17:26:55 by vflorez ### ########.fr #
# Updated: 2023/12/19 12:47:55 by vflorez ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -29,7 +29,7 @@ SRC_FILES = src/swap_actions.c\
src/reverse_rot_actions.c\
src/stack_utils.c\
src/main.c\
src/error.c\
src/free.c\
src/sort_3.c\
src/handle_args.c

Expand Down
7 changes: 4 additions & 3 deletions includes/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vflorez <vflorez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 16:14:44 by vflorez #+# #+# */
/* Updated: 2023/12/18 19:12:24 by vflorez ### ########.fr */
/* Updated: 2023/12/19 17:33:03 by vflorez ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,12 +38,13 @@ void ft_free(t_stack_node *stack);
/*Stacks Utils*/
int stack_size(t_stack_node *stack);
void *add_node_end(t_stack_node **stack, t_stack_node *newnode);
void printStack(const char *name, t_stack_node* stack);
void printStack(const char *name, t_stack_node* stack);
void add_data(t_stack_node **stack, int data);
void ft_stack_organized(t_stack_node *stack);
void ft_node_index(t_stack_node **stack_a, int data);


/*Actions allowed*/
/*Actions allowed*/
void ft_sa(t_stack_node **stack_a);
void ft_sb(t_stack_node **stack_b);
void ft_ss(t_stack_node **stack_a, t_stack_node **stack_b);
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vflorez <vflorez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 20:06:07 by vflorez #+# #+# */
/* Updated: 2023/12/18 17:37:40 by vflorez ### ########.fr */
/* Updated: 2023/12/19 17:49:33 by vflorez ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -76,7 +76,7 @@ int main(int argc, char *argv[])
while (i < argc)
{
int value = ft_atoi(argv[i]);
add_data(&stack_a, value);
ft_node_index(&stack_a, value);
if(!check_limits(&stack_a) || !check_duplicate(&stack_a) || !check_str(argv[i]))
{
ft_printf("invalid input");
Expand All @@ -88,7 +88,7 @@ int main(int argc, char *argv[])

printStack("Inicial A", stack_a);

ft_stack_organized(stack_a);
ft_sort_3(&stack_a);

printStack("Final A", stack_a);
ft_free(stack_a);
Expand Down
62 changes: 56 additions & 6 deletions src/stack_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vflorez <vflorez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/14 08:12:33 by vradis #+# #+# */
/* Updated: 2023/12/18 17:38:04 by vflorez ### ########.fr */
/* Updated: 2023/12/19 17:47:59 by vflorez ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -62,13 +62,13 @@ void printStack(const char *name, t_stack_node* stack)
{
ft_printf("Stack %s: ", name);
while (stack != NULL) {
ft_printf("%d -> ", stack->data);
ft_printf("data: %d\n -> ", stack->data);
ft_printf("index : %d\n -> ", stack->index);
stack = stack->next;
}
ft_printf("NULL\n");
}

/*Functions to add argv to stacks*/
void add_data(t_stack_node **stack, int data)
{
t_stack_node *newnode;
Expand All @@ -78,6 +78,55 @@ void add_data(t_stack_node **stack, int data)
*stack = newnode;
}

/*Function creates a new node to add in stack and
initialize the alias index in 1 */
static t_stack_node *add_newnode(t_stack_node *stack_a, int data)
{
t_stack_node *newnode;
newnode = malloc(sizeof(t_stack_node));
if (newnode == NULL)
{
ft_free(stack_a);
ft_printf("Error\n");
}
newnode->data = data;
newnode->index = 1;
newnode->next = NULL;
return (newnode);
}

/*Functions:
1. Create a newnode
2. Create the alias index in each node or data and it will help
to organize the stack */

void ft_node_index(t_stack_node **stack_a, int data)
{
t_stack_node *newnode;
t_stack_node *ptr;

newnode = add_newnode(*stack_a, data);
if(*stack_a == NULL)
*stack_a = newnode;
else
{
ptr = *stack_a;
if(ptr->data > newnode->data)
ptr->index++;
else
newnode->index++;
while(ptr->next != NULL)
{
ptr = ptr->next;
if(ptr->data > newnode->data)
ptr->index++;
else
newnode->index++;
}
ptr->next = newnode;
}
}


/*Function that checks if the numbers in the stack are organized
from smallest to largest
Expand All @@ -92,8 +141,9 @@ void ft_stack_organized(t_stack_node *stack)
while(ptr != NULL)
{
if(ptr->next != NULL && ptr->index > ptr->next->index)
ft_printf("stack is not organized\n");
ptr = ptr->next;
ft_printf("stack is not organized\n");
ptr = ptr->next;
}
ft_printf("stack is organized\n");
}
}

0 comments on commit 1d0785d

Please sign in to comment.