void append_value_linked_list(struct linked_list_t *linked_list, void *value) { /* allocates space for the linked list node */ struct linked_list_node_t *linked_list_node; /* creates the linked list node */ create_linked_list_node(&linked_list_node); /* sets the value in the linked list node */ linked_list_node->value = value; /* appends the linked list node to the linked list */ append_linked_list(linked_list, linked_list_node); }
void append_index_linked_list( struct linked_list_t *linked_list, struct linked_list_node_t *linked_list_node, size_t index ) { /* allocates the space for the temporary index counter value and for the pointers to the previous and next nodes from which the new node will be inserted in between */ size_t _index; struct linked_list_node_t *previous = NULL; struct linked_list_node_t *next = linked_list->first; /* in case the provided index is greater that the current linked list size it's an invalid operation and so must return immediately */ if(index > linked_list->size) { return; } /* in case the current index is the current size a normal append operation must be done as this is a special case and the general logic must be performed instead of the index one */ if(index == linked_list->size) { append_linked_list(linked_list, linked_list_node); return; } /* iterates over the range of index to gather the next and previous pointers to the required slot for insertion, these are the nodes that are going to be updated for the insertion operation */ for(_index = 0; _index < index; _index++) { next = next->next; previous = next->previous; } /* in case either the current index is the first one or the last one the first and last pointer references must be updated in the linked list */ if(index == 0) { linked_list->first = linked_list_node; } if(index == linked_list->size) { linked_list->last = linked_list_node; } /* updates the next and and previous references in the linked list node to be inserted to point to the gathered values */ linked_list_node->next = next; linked_list_node->previous = previous; /* updates the next and previous node references so that they are aware of the new node that is going to be inserted in the middle of them */ if(next) { next->previous = linked_list_node; } if(previous) { previous->next = linked_list_node; } /* increments the current linked list size indicating that a new node has been inserted in the current linked list */ linked_list->size++; }
LinkedList *create_linked_list_from_array(unsigned long data_size, void *array, int n){ /** * Creates and returns a linked list by reading n objects of size data_size * from the array indicated by the pointer. */ LinkedList *list = create_linked_list(data_size); int i; for(i = 0; i < n; i++){ //allocate memory for data copy void *item = malloc(data_size); void *address = array + i*data_size; memcpy(item, address, data_size); append_linked_list(list, item); } return list; }