Skip to content

Commit

Permalink
gotta lower op
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana Laura Da Silva Volkmann committed Jul 19, 2024
1 parent 08efcbd commit 7187ee0
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 57 deletions.
58 changes: 58 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\app.js"
}
]
}
22 changes: 14 additions & 8 deletions ft_big_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 12:54:58 by ana-lda- #+# #+# */
/* Updated: 2024/07/17 14:47:07 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 11:18:38 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -72,14 +72,20 @@ void send_b_to_a(t_stack **stack_a, t_stack **stack_b)
ft_pa(stack_a, stack_b, 0);
}

/* void lowest_on_top(t_stack **stack_a)
void lowest_on_top(t_stack **stack_a)
{
if (!is_sorted(*stack_a))
while (*stack_a != ft_lowest(stack_a))
{
t_stack *lowest;

index_median(stack_a);
lowest = ft_lowest(stack_a);
while (lowest != *stack_a)
{
if (lowest->above_median)
ft_ra(stack_a, 0);
}
} */
else
ft_rra(stack_a, 0);
}
}
void sort_stack(t_stack **stack_a, t_stack **stack_b)
{
if (ft_list_size(*stack_a) == 2)
Expand All @@ -101,5 +107,5 @@ void sort_stack(t_stack **stack_a, t_stack **stack_b)
update_stack_nodes(stack_a, stack_b, 0);
send_b_to_a(stack_a, stack_b);
}
//lowest_on_top
lowest_on_top(stack_a);
}
10 changes: 6 additions & 4 deletions list_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 17:56:45 by ana-lda- #+# #+# */
/* Updated: 2024/07/16 13:21:39 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 13:48:24 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,14 +57,16 @@ t_stack *ft_last_list(t_stack *lst)
}

/** @brief Conts the number of elements in the stack*/
int ft_list_size(t_stack *lst)
int ft_list_size(t_stack *stack)
{
size_t count;
t_stack *temp;

temp = stack;
count = 0;
while (lst)
while (temp)
{
lst = lst->next;
temp = temp->next;
count++;
}
return (count);
Expand Down
54 changes: 34 additions & 20 deletions operations_stack_a.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 17:33:07 by ana-lda- #+# #+# */
/* Updated: 2024/07/13 18:55:19 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 14:58:15 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,14 +17,17 @@
void ft_ra(t_stack **stack_a, int i)
{
t_stack *temp;
t_stack *last;

if (!*stack_a || !(*stack_a)->next)
return ;
temp = *stack_a;
*stack_a = ft_last_list(*stack_a);
(*stack_a)->next = temp;
last = ft_last_list(*stack_a);
*stack_a = temp->next;
(*stack_a)->prev = NULL;
temp->next = NULL;
temp->prev = last;
last->next = temp;
if (i == 0)
write(1, "ra\n", 3);
}
Expand All @@ -33,14 +36,20 @@ void ft_ra(t_stack **stack_a, int i)
Do nothing if there is only one or no elements.*/
void ft_sa(t_stack **stack_a, int i)
{
t_stack *temp;
t_stack *first;
t_stack *second;

if (!*stack_a || !((*stack_a)->next))
return ;
temp = *stack_a;
*stack_a = (*stack_a)->next;
temp->next = (*stack_a)->next;
(*stack_a)->next = temp;
first = *stack_a;
second = first->next;
first->next = second->next;
if (second->next)
second->next->prev = first;
first->prev = second;
second->next = first;
second->prev = NULL;
(*stack_a) = second;
if (i == 0)
write (1, "sa\n", 3);
}
Expand All @@ -49,16 +58,17 @@ void ft_sa(t_stack **stack_a, int i)
The last element becomes the first one.*/
void ft_rra(t_stack **stack_a, int i)
{
t_stack *temp;
t_stack *last;

if (!*stack_a || !((*stack_a)->next))
return ;
temp = ft_last_list(*stack_a);
temp->next = *stack_a;
temp->prev->next = NULL;
(*stack_a)->prev = temp;
*stack_a = temp;
(*stack_a)->prev = NULL;
last = ft_last_list(*stack_a);
if (last->prev)
last->prev->next = NULL;
last->next = *stack_a;
(*stack_a)->prev = last;
last->prev = NULL;
(*stack_a) = last;
if (i == 0)
write (1, "rra\n", 4);
}
Expand All @@ -67,14 +77,18 @@ void ft_rra(t_stack **stack_a, int i)
place it on top of 'a'. Do nothing if 'b' is empty.*/
void ft_pa(t_stack **stack_a, t_stack **stack_b, int i)
{
t_stack *temp;
t_stack *temp_b;

if (!stack_b)
return ;
temp = *stack_a;
*stack_a = *stack_b;
*stack_b = (*stack_b)->next;
(*stack_a)->next = temp;
temp_b = *stack_b;
*stack_b = temp_b->next;
if (*stack_b)
(*stack_b)->prev = NULL;
temp_b->next = *stack_a;
if (*stack_a)
(*stack_a)->prev = temp_b;
*stack_a = temp_b;
if (i == 0)
write(1, "pa\n", 3);
}
41 changes: 24 additions & 17 deletions operations_stack_b.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 12:45:59 by ana-lda- #+# #+# */
/* Updated: 2024/07/17 14:12:27 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 14:49:16 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,14 +17,17 @@
void ft_rb(t_stack **stack_b, int i)
{
t_stack *temp;
t_stack *last;

if (!*stack_b || !(*stack_b)->next)
return ;
temp = *stack_b;
*stack_b = ft_last_list(*stack_b);
(*stack_b)->next = temp;
last = ft_last_list(*stack_b);
*stack_b = temp->next;
(*stack_b)->prev = NULL;
temp->next = NULL;
temp->prev = last;
last->next = temp;
if (i == 0)
write(1, "ra\n", 3);
}
Expand All @@ -33,14 +36,19 @@ void ft_rb(t_stack **stack_b, int i)
Do nothing if there is only one or no elements.*/
void ft_sb(t_stack **stack_b, int i)
{
t_stack *temp;
t_stack *second;
t_stack *first;

if (!*stack_b || !((*stack_b)->next))
return ;
temp = *stack_b;
*stack_b = (*stack_b)->next;
temp->next = (*stack_b)->next;
(*stack_b)->next = temp;
first = *stack_b;
second = first->next;
first->next = second->next;
if (second->next)
second->next->prev = first;
second->next = first;
second->prev = NULL;
(*stack_b) = second;
if (i == 0)
write (1, "sb\n", 3);
}
Expand All @@ -49,16 +57,17 @@ void ft_sb(t_stack **stack_b, int i)
The last element becomes the first one.*/
void ft_rrb(t_stack **stack_b, int i)
{
t_stack *temp;
t_stack *last;

if (!*stack_b || !((*stack_b)->next))
return ;
temp = ft_last_list(*stack_b);
temp->next = *stack_b;
temp->prev->next = NULL;
(*stack_b)->prev = temp;
*stack_b = temp;
(*stack_b)->prev = NULL;
last = ft_last_list(*stack_b);
if (last->prev)
last->prev->next = NULL;
last->next = *stack_b;
(*stack_b)->prev = last;
last->prev = NULL;
*stack_b = last;
if (i == 0)
write (1, "rrb\n", 4);
}
Expand All @@ -77,9 +86,7 @@ void ft_pb(t_stack **stack_b, t_stack **stack_a, int i)
*stack_b = temp_a;
*stack_a = (temp_a)->next;
if (*stack_a)
{
(*stack_a)->prev = NULL;
}
(*stack_b)->next = temp;
if (temp)
temp->prev = *stack_b;
Expand Down
7 changes: 4 additions & 3 deletions push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 15:05:43 by ana-lda- #+# #+# */
/* Updated: 2024/07/17 14:15:41 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 11:16:31 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -78,11 +78,12 @@ void ft_rrr(t_stack **stack_a, t_stack **stack_b, int i);

void sort_stack(t_stack **stack_a, t_stack **stack_b);
void sort_three(t_stack **stack_a);
void lowest_on_top(t_stack **stack_a);

/*......................SET_TARGETS........................*/

void set_target_in_b(t_stack **stack_a, t_stack **stack_b);
void set_target_in_a(t_stack **stack_a, t_stack **stack_b);
void set_target_in_b(t_stack **stack_a, t_stack **stack_b);
void set_target_in_a(t_stack **stack_a, t_stack **stack_b);
void update_stack_nodes(t_stack **stack_a, t_stack **stack_b, int flag);

/*......................SET_COST_OF.........................*/
Expand Down
4 changes: 3 additions & 1 deletion set_cost_of.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 13:38:28 by ana-lda- #+# #+# */
/* Updated: 2024/07/17 15:03:00 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 13:58:04 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,8 @@ void index_median(t_stack **stack)

median = ft_list_size(*stack) / 2;
i = 0;
while ((*stack)->prev)
*stack = (*stack)->prev;
temp = *stack;
while (temp)
{
Expand Down
8 changes: 4 additions & 4 deletions set_targets.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/13 14:26:47 by ana-lda- #+# #+# */
/* Updated: 2024/07/17 15:13:20 by ana-lda- ### ########.fr */
/* Updated: 2024/07/19 12:07:23 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,10 +49,10 @@ void set_target_in_a(t_stack **stack_a, t_stack **stack_b)
t_stack *temp_a;
t_stack *temp_b;
t_stack *best_target;
t_stack *biggest;
t_stack *lowest;

temp_b = *stack_b;
biggest = ft_biggest(stack_a);
lowest = ft_lowest(stack_a);
while (temp_b)
{
best_target = NULL;
Expand All @@ -65,7 +65,7 @@ void set_target_in_a(t_stack **stack_a, t_stack **stack_b)
temp_a = temp_a->next;
}
if (!best_target)
temp_b->target = biggest;
temp_b->target = lowest;
else
temp_b->target = best_target;
temp_b = temp_b->next;
Expand Down

0 comments on commit 7187ee0

Please sign in to comment.