Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions includes/libft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
# include <stdlib.h>
# include <string.h>

/*
** 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
16 changes: 16 additions & 0 deletions includes/push_swap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
67 changes: 67 additions & 0 deletions srcs/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ztisnes <ztisnes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}