Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • fd4f7aa9e52ecb4d10a885006a2d857fe39f7682
  • arise par défaut
  • list
3 résultats

bot.pl

Blame
  • stack.c 1,41 Kio
    #define _POSIX_C_SOURCE 200809L
    #include <common/stack.h>
    #include <common/common.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int
    stack_new(struct stack *ret)
    {
        if (!ret)
            return 1;
    
        struct stack _ret = {
            .contents = malloc(LKT_DEFAULT_LIST_SIZE * sizeof(void *)),
            .size = LKT_DEFAULT_LIST_SIZE,
            .len = 0,
        };
    
        if (_ret.contents == NULL)
            return 1;
    
        *ret = _ret;
        return 0;
    }
    
    void
    stack_free(struct stack *lst)
    {
        if (!lst)
            return;
    
        if (lst->contents)
            free(lst->contents);
    
        lst->size = 0;
        lst->len  = 0;
    }
    
    int
    stack_empty(struct stack *lst)
    {
        if (!lst || !lst->contents || !lst->len)
            return 1;
        return 0;
    }
    
    int
    stack_push(struct stack *lst, void *item)
    {
        if (!lst)
            return 1;
    
        if (lst->size == lst->len) {
            volatile void **new = realloc(lst->contents,
                                          lst->size * 2 * sizeof(void *));
    
            if (NULL == new)
                return 1;
    
            lst->contents = new;
            lst->size *= 2;
        }
    
        lst->contents[(lst->len)++] = item;
        return 0;
    }
    
    int
    stack_pop(struct stack *lst, void **item)
    {
        if (!lst || !lst->len)
            return 1;
    
        *item = (void *) lst->contents[--(lst->len)];
        return 0;
    }
    
    int
    stack_head(struct stack *lst, void **item)
    {
        if (!lst || !lst->len)
            return 1;
    
        *item = (void *) lst->contents[lst->len - 1];
        return 0;
    }