-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
366 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* debug.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/04/04 17:30:25 by ablaamim #+# #+# */ | ||
/* Updated: 2022/04/04 17:39:57 by ablaamim ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/push_swap.h" | ||
|
||
void print_stacks(t_stacks stacks) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
ft_printf("\nsize:\t%d\t%d\n-----\nstack:", stacks.a.size, stacks.b.size); | ||
while (i < stacks.a.size || i < stacks.b.size) | ||
{ | ||
if (i < stacks.a.size) | ||
{ | ||
ft_printf("\t%d", stacks.a.head->nbr); | ||
if (stacks.a.head->next) | ||
stacks.a.head = stacks.a.head->next; | ||
} | ||
else | ||
ft_printf("\t"); | ||
if (i < stacks.b.size) | ||
{ | ||
ft_printf("\t%d", stacks.b.head->nbr); | ||
if (stacks.b.head->next) | ||
stacks.b.head = stacks.b.head->next; | ||
} | ||
ft_printf("\n"); | ||
i++; | ||
} | ||
ft_printf("\t_\t_\n\ta\tb\n> "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* exit.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/04/04 17:07:00 by ablaamim #+# #+# */ | ||
/* Updated: 2022/04/04 17:14:18 by ablaamim ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/push_swap.h" | ||
|
||
void ft_stack_free(t_node **lst, int size) | ||
{ | ||
t_node *to_free; | ||
int i; | ||
|
||
to_free = *lst; | ||
if (!lst || *lst) | ||
return ; | ||
i = 0x0; | ||
while (i < size) | ||
{ | ||
*lst = to_free->next; | ||
free(to_free); | ||
to_free = *lst; | ||
i++; | ||
} | ||
*lst = NULL; | ||
} | ||
|
||
void ft_clear_stacks(t_stacks *stacks) | ||
{ | ||
ft_stack_free(&stacks->a.head, stacks->a.size); | ||
ft_stack_free(&stacks->b.head, stacks->a.size); | ||
} | ||
|
||
void exit_program(t_stacks *stacks) | ||
{ | ||
write(2, "Error\n", 6); | ||
ft_clear_stacks(stacks); | ||
exit(EXIT_FAILURE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_printf.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/04/04 17:22:13 by ablaamim #+# #+# */ | ||
/* Updated: 2022/04/04 17:28:14 by ablaamim ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/push_swap.h" | ||
|
||
int ft_putchar_printf(char c) | ||
{ | ||
return (write(1, &c, 1)); | ||
} | ||
|
||
int ft_putstr_printf(char *str) | ||
{ | ||
int len; | ||
|
||
len = 0x0; | ||
if (str == NULL) | ||
{ | ||
len += write(1, "(null)", 6); | ||
return (len); | ||
} | ||
while (*str) | ||
len += ft_putchar_printf(*str++); | ||
return (len); | ||
} | ||
#define hex "0123456789abcdef" | ||
int ft_putnbr_printf(long long nb, int base) | ||
{ | ||
int len = 0x0; | ||
if (nb < 0) | ||
{ | ||
nb = -nb; | ||
len+=ft_putchar_printf('-') | ||
;} | ||
if (nb >= base) | ||
len += ft_putnbr_printf(nb / base, base); | ||
len += ft_putchar_printf(hex[nb % base]); | ||
return (len); | ||
} | ||
int ft_vaprintf(va_list ap, const char *fmt) | ||
{ | ||
int len = 0x0; char c; | ||
while (*fmt) | ||
{ | ||
c = *fmt++; | ||
if (c != '%') | ||
len += ft_putchar_printf(c); | ||
else | ||
{ | ||
c = *fmt++; | ||
if (c == 's') | ||
len += ft_putstr_printf(va_arg(ap, char *)); | ||
if (c == 'd') | ||
len+=ft_putnbr_printf((long long)va_arg(ap, int), 10); | ||
} | ||
} | ||
return (len); | ||
} | ||
|
||
int ft_printf(const char *fmt, ...) | ||
{ | ||
int len; | ||
va_list ap; | ||
|
||
len = 0x0; | ||
va_start(ap, fmt); | ||
len += ft_vaprintf(ap, fmt); | ||
va_end(ap); | ||
return (len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_stacks_constructor.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ablaamim <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/04/04 16:48:45 by ablaamim #+# #+# */ | ||
/* Updated: 2022/04/04 17:22:00 by ablaamim ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/push_swap.h" | ||
|
||
t_node *ft_create_node(int numb) | ||
{ | ||
t_node *elem; | ||
|
||
elem = ft_calloc(0x1, sizeof(t_node)); | ||
if (!elem) | ||
return (0x0); | ||
elem->nbr = numb; | ||
elem->next = NULL; | ||
return (elem); | ||
} | ||
|
||
void ft_nodeadd_back(t_node **lst, t_node *new) | ||
{ | ||
t_node *tmp; | ||
|
||
tmp = *lst; | ||
if (!new) | ||
return ; | ||
if (!*lst) | ||
*lst = new; | ||
else | ||
{ | ||
while (tmp->next) | ||
tmp = tmp->next; | ||
tmp->next = new; | ||
} | ||
} | ||
|
||
void ft_stacks_constructor(char **argv, t_stacks *stacks) | ||
{ | ||
t_node *new_node; | ||
|
||
stacks->a = (t_stack) {0}; | ||
stacks->b = (t_stack) {0}; | ||
while (argv[stacks->a.size]) | ||
{ | ||
new_node = ft_create_node(ft_atoi(argv[stacks->a.size])); | ||
if (!new_node) | ||
exit_program(stacks); | ||
ft_nodeadd_back(&stacks->a.head, new_node); | ||
(stacks->a.size)++; | ||
} | ||
} |
Oops, something went wrong.