Ejemplo n.º 1
0
void delete_linked_list(struct linked_list_t *linked_list) {
    /* allocates space for the index and for the
    temporary next node variable */
    unsigned int index;
    struct linked_list_node_t *next_node;

    /* sets the initial iteration node as
    the first element from the linked list */
    struct linked_list_node_t *current_node = linked_list->first;

    /* iterates over the complete set of nodes in
    the linked list (list range) to delete them */
    for(index = 0; index < linked_list->size; index++) {
        /* retrieves the next node and deletes the current one
        then sets the next node as the current one in order
        to be able to continue the iteration */
        next_node = current_node->next;
        delete_linked_list_node(current_node);
        current_node = next_node;
    }

    /* releases the linked list avoiding ny memory
    leak from the internal structure */
    FREE(linked_list);
}
Ejemplo n.º 2
0
void clear_linked_list(struct linked_list_t *linked_list) {
    /* allocates space for the index and for the
    temporary next node variable */
    unsigned int index;
    struct linked_list_node_t *next_node;

    /* sets the initial iteration node as
    the first element from the linked list */
    struct linked_list_node_t *current_node = linked_list->first;

    /* iterates over the complete set of nodes in
    the linked list (list range) to delete them */
    for(index = 0; index < linked_list->size; index++) {
        /* retrieves the next node and deletes the current one
        then sets the next node as the current one in order
        to be able to continue the iteration */
        next_node = current_node->next;
        delete_linked_list_node(current_node);
        current_node = next_node;
    }

    /* resets the linked list size to the original
    empty value of zero */
    linked_list->size = 0;
}
Ejemplo n.º 3
0
void *list_delete_pos(struct linked_list *list, int pos){
    if(pos>=list->count||pos<(-list->count))
        return NULL;
    int i;
    void *data;
    struct linked_list_node *xnode;
    xnode = list->first_node;
    if(pos<0){
        for(i=0;i<(-pos);++i)
            xnode = xnode->previous;
        data = xnode->data;
        delete_linked_list_node(list,xnode);
        list->count -= 1;
        return data;
    }
    else{
        for(i=0;i<pos;++i)
            xnode = xnode->next;
        data = xnode->data;
        delete_linked_list_node(list,xnode);
        list->count -= 1;
        return data;
    }
}
Ejemplo n.º 4
0
void remove_linked_list(struct linked_list_t *linked_list, struct linked_list_node_t *linked_list_node, char delete_node) {
    /* allocates space for the previous node */
    struct linked_list_node_t *previous_node;

    /* allocates space for the next node */
    struct linked_list_node_t *next_node;

    /* in case the linked list node is invalid
    must return immediately to avoid problems */
    if(linked_list_node == NULL) { return; }

    /* retrieves both the previous and the next nodes */
    previous_node = linked_list_node->previous;
    next_node = linked_list_node->next;

    /* in case the previous node is valid sets the
    next node in the previous node */
    if(previous_node != NULL) { previous_node->next = next_node; }

    /* in case the next node is valid updates the
    previous node in the next node to previous node */
    if(next_node != NULL) { next_node->previous = previous_node; }

    /* in case the element to be removed is the last
    (no next node defined) must update the last node
    reference in the linked list */
    if(next_node == NULL) { linked_list->last = previous_node; }

    /* in case the element to be removed is the last minus one
    must update the last node in the linked list to the next node */
    else if(next_node != NULL && next_node->next == NULL) {
        linked_list->last = next_node;
    }

    /* in case the element to be removed is the first (no previous
    node is defined) must set the next node as the first node */
    if(previous_node == NULL) { linked_list->first = next_node; }

    /* decrements the linked list size, because the linked list
    has shrinked in size by one */
    linked_list->size--;

    /* in case the delete node flag is set, must remote the
    memory allocated for the linked list node */
    if(delete_node) { delete_linked_list_node(linked_list_node); }
}
Ejemplo n.º 5
0
void *list_delete_func(struct linked_list *list, void *func){
    int (*xfunc)(void *) = func;
    int i;
    struct linked_list_node *node;
    void *data;
    node = list->first_node;
    for(i=0;i<list->count;++i){
        if(!xfunc(node->data)){
            data = node->data;
            delete_linked_list_node(list, node);
            list->count -= 1;
            return data;
        }
        node = node->next;
    }
    return NULL;
}
Ejemplo n.º 6
0
void pop_top_value_linked_list(struct linked_list_t *linked_list, void **value_pointer, char delete_node) {
    /* allocates space for the linked list node */
    struct linked_list_node_t *linked_list_node;

    /* pops top the linked list node */
    pop_top_linked_list(linked_list, &linked_list_node);

    /* in case the linked list node is invalid */
    if(linked_list_node == NULL) {
        /* sets the null valie in the value pointer */
        *value_pointer = NULL;
    } else {
        /* sets the linked list node value in the value pointer */
        *value_pointer = linked_list_node->value;
    }

    /* in case the linked list node is valid
    and the delete node flag is set */
    if(linked_list_node != NULL && delete_node) {
        /* deletes the linked list node */
        delete_linked_list_node(linked_list_node);
    }
}