From 925c50a9b094f6d244b621c8750d5ecf0caa38a1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 12 Apr 2024 20:51:04 +0200 Subject: [PATCH] Add error handling and create base for commands --- command_handling.c | 48 +++++++++++++++++++++++++++++++++++++++++ main.c | 12 ++++++++--- stack_utils.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 command_handling.c create mode 100644 stack_utils.c diff --git a/command_handling.c b/command_handling.c new file mode 100644 index 0000000..484c858 --- /dev/null +++ b/command_handling.c @@ -0,0 +1,48 @@ +#include "libft/libft.h" +#include "push_swap.h" + +static int add_cmd_to_queue(t_list **cmds, enum e_pscmd cmd) +{ + t_list *new_elem; + enum e_pscmd *ptr_cmd; + + ptr_cmd = malloc(sizeof(enum e_pscmd)); + if (!ptr_cmd) + return 1; + + new_elem = ft_lstnew(ptr_cmd); + if (!new_elem) + { + free(ptr_cmd); + return 1; + } + ft_lstadd_back(cmds, new_elem); + return 0; +} + +void run_command(t_list **stack_a, t_list **stack_b, t_list **cmds, enum e_pscmd cmd) +{ + if (cmd == SA) + ; + else if (cmd == SB) + ; + else if (cmd == SS) + ; + else if (cmd == PA) + ; + else if (cmd == PB) + ; + else if (cmd == RA) + ; + else if (cmd == RB) + ; + else if (cmd == RR) + ; + else if (cmd == RRA) + ; + else if (cmd == RRB) + ; + else if (cmd == RRR) + ; + add_cmd_to_queue(cmds, cmd); +} diff --git a/main.c b/main.c index 2643ec2..e9a96e1 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: dkaiser next; + first_elem->next = (*stack)->next; + (*stack)->next = first_elem; +} + +void stack_push(t_list **dst_stack, t_list **src_stack) +{ + t_list *elem; + + elem = *src_stack; + *src_stack = elem->next; + ft_lstadd_front(dst_stack, elem); +} + +void stack_rotate(t_list **stack) +{ + t_list *first_elem; + + first_elem = *stack; + *stack = (*stack)->next; + first_elem->next = NULL; + ft_lstlast(*stack)->next = first_elem; +} + +void stack_rrotate(t_list **stack) +{ + t_list *first_elem; + + first_elem = *stack; + while((*stack)->next->next) + *stack = (*stack)->next; + (*stack)->next->next = first_elem; + (*stack)->next = NULL; +} -- 2.47.2