- allowed functions : malloc, free
- linked list๋ฅผ ์ฌ์ฉํ์ฌ stack์ ๊ตฌํ ํฉ๋๋ค.
- ์๋์ ๊ฐ์ stack.h๋ฅผ ์ฌ์ฉ ํฉ๋๋ค.
typedef struct s_node { void *data; struct s_node *next; } t_node; typedef struct s_stack { unsigned int size; t_node *top; } t_stack;
- t_stackํ struct๋ฅผ ๋ฐํ ํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
- ๋ฐํ๋๋ t_stack๋ ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ์ด๊ธฐํ๋ฅผ ๊ฑฐ์ณ์ผํฉ๋๋ค.
t_stack *stack_init(void);
- t_nodeํ ์๋ก์ด ์์๋ฅผ ์์ฑํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
t_node *create_elem(void *data);
- stack์ ๊ฐ์ฅ ์์ data๋ฅผ ๊ฐ๋ ์๋ก์ด ์์๋ฅผ ์์ฑํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
- ์ฑ๊ณตํ๋ฉด 1, ์คํจํ๋ฉด 0์ ๋ฐํ ํฉ๋๋ค.
int stack_push(t_stack *stack, void *data);
- stack์ ์๋ ์์์ ๊ฐ์๋ฅผ ๋ฐํํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
int stack_size(t_stack *stack);
- stack์์ ๊ฐ์ฅ ์์ ์๋ ์์๋ฅผ ๋ฐํํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
t_node *stack_peek(t_stack *stack);
- stack์์ ๊ฐ์ฅ ์์ ์๋ ์์๋ฅผ ๊บผ๋ด๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
- ๋ฐํํ๋ ์์์ next๋ฅผ ๋ํฌ์ธํฐ๋ก ๋ฐ๊พผํ ๋ฐํ ํฉ๋๋ค.
t_node *stack_pop(t_stack *stack);
- stack์ ์์ ์ ์ฒด๋ฅผ ์ญ์ ํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
- ์์์ ๋ฐ์ดํฐ๋ free_data๋ฅผ ์ฌ์ฉํด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํด์ ํด์ผ ํฉ๋๋ค.
void stack_clear(t_stack *stack, void (*free_data)(void *));
- stack์ ์๋ ์์ ์ ์ฒด๋ฅผ ์ญ์ ํ๊ณ stack์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํด์ ํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์.
- ์์์ ๋ฐ์ดํฐ๋ free_data๋ฅผ ์ฌ์ฉํด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํด์ ํด์ผ ํฉ๋๋ค.
void free_stack(t_stack *stack, void (*free_data)(void *));