​push_swap /
src / stack / stack_lifecycle.c
162 commits
02-05-2026
44 lines 1.4 kB
 1/* ************************************************************************** */
 2/*                                                                            */
 3/*                                                        :::      ::::::::   */
 4/*   stack_lifecycle.c                                  :+:      :+:    :+:   */
 5/*                                                    +:+ +:+         +:+     */
 6/*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7/*                                                +#+#+#+#+#+   +#+           */
 8/*   Created: 2026/04/21 23:42:42 by mleitao           #+#    #+#             */
 9/*   Updated: 2026/05/02 15:25:19 by mleitao          ###   ########.fr       */
10/*                                                                            */
11/* ************************************************************************** */
12
13#include "stack.h"
14#include <libft.h>
15#include <stdlib.h>
16
17int	stack_init(t_stack *s, int capacity)
18{
19	s->buf = malloc(sizeof(int) * capacity);
20	if (!s->buf)
21		return (-1);
22	s->capacity = capacity;
23	s->size = 0;
24	s->head = 0;
25	return (0);
26}
27
28int	stack_build(t_stack *s, const int *values, int capacity)
29{
30	if (stack_init(s, capacity) == -1)
31		return (-1);
32	ft_memcpy(s->buf, values, sizeof(int) * capacity);
33	s->size = s->capacity;
34	return (0);
35}
36
37void	stack_free(t_stack *s)
38{
39	free(s->buf);
40	s->buf = NULL;
41	s->size = 0;
42	s->head = 0;
43	s->capacity = 0;
44}