diff --git a/includes/libft.h b/includes/libft.h new file mode 100644 index 0000000..f3f0871 --- /dev/null +++ b/includes/libft.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ztisnes +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/09/21 21:47:24 by ztisnes #+# #+# */ +/* Updated: 2018/02/03 18:35:41 by ztisnes ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include + +/* +** Linked List structure +*/ + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +t_stack *init_stack(void); +void push_stack(t_stack *stack, void *content); +void *pop_stack(t_stack *stack); +void *peek(t_stack *stack); +int isEmpty_stack(t_stack *stack); +#endif diff --git a/includes/push_swap.h b/includes/push_swap.h new file mode 100644 index 0000000..27f4684 --- /dev/null +++ b/includes/push_swap.h @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ztisnes +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/02/03 18:34:37 by ztisnes #+# #+# */ +/* Updated: 2018/02/03 18:35:56 by ztisnes ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUSH_SWAP_H +# define PUSH_SWAP_H + +#endif diff --git a/srcs/stack.c b/srcs/stack.c new file mode 100644 index 0000000..9556282 --- /dev/null +++ b/srcs/stack.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ztisnes +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/01/09 19:57:20 by ztisnes #+# #+# */ +/* Updated: 2018/02/03 17:48:15 by ztisnes ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** Stacks are abstract data structures which helps in the order of elements with +** popping (removing) and pushing (inserting) elements Stacks are just arrays +** +*/ +typedef struct s_stack +{ + struct s_list *top; +} t_stack; + +t_stack *init_stack(void) +{ + t_stack *node; + node = (t_stack *)malloc(sizeof(t_stack)); + node->top = NULL; + return (node); +} + +void push_stack(t_stack *stack, void *content) +{ + t_list *node; + + node = (t_list *)malloc(sizeof(t_list)); + node->content = content; + node->next = stack->top; + stack->top = node; +} + +void *pop_stack(t_stack *stack) +{ + t_list *next; + void *anything; + + if (stack->top == NULL) + return (NULL); + next = stack->top->next; + anything = stack->top->content; + free(stack->top); + stack->top = next; + return (anything); +} + +void *peek_stack(t_stack *stack) +{ + if (stack->top == NULL) + return (NULL); + return (stack->top->content); +} + +int isEmpty_stack(t_stack *stack) +{ + return (stack->top == NULL); +}