digraph/linked_list.c
/*
 * The linked_list_remove function removes an element fron the linledlist struct
 * at a specific index. The data value stored at this index is returned to the caller.
 */
The linked_list_remove function removes an element fron the linledlist struct
at a specific index. The data value stored at this index is returned to the caller.
The linked_list_remove function removes an element fron the linledlist struct at a specific index . The data value stored at this index is returned to the caller .
void* linked_list_remove(LinkedList* list, int index)
{
	if(list == NULL || index < 0 || index >= list->size)
		return NULL;
	void* element = malloc(list->itemSize);
	if(list->size == 1)
	{
		memcpy(element,list->first->data, list->itemSize);
		list->first = NULL;
		list->last  = NULL;
		list->size--;
		return element;
	}
	else if(index == 0)
	{
		memcpy(element, list->first->data, list->itemSize);
		list->first       = list->first->next;
		list->first->prev = NULL;
		list->size--;
		return element;
	}
	else if(index == list->size - 1)
	{
		memcpy(element, list->last->data, list->itemSize);
		list->last       = list->last->prev;
		list->last->next = NULL;
		list->size--;
		return element;
	}
	else
	{
		Node* temp = list->first;
		for(int i = 0; i < index; i++)
			temp = temp->next;
		memcpy(element, temp->data, list->itemSize);
		temp->prev->next = temp->next;
		temp->next->prev = temp->prev;
		list->size--;
		return element;
	}
}
void * linked_list_remove ( LinkedList * list , int index ) { if ( list == NULL || index < 0 || index >= list -> size ) return NULL ; void * element = malloc ( list -> itemSize ) ; if ( list -> size == 1 ) { memcpy ( element , list -> first -> data , list -> itemSize ) ; list -> first = NULL ; list -> last = NULL ; list -> size -- ; return element ; } else if ( index == 0 ) { memcpy ( element , list -> first -> data , list -> itemSize ) ; list -> first = list -> first -> next ; list -> first -> prev = NULL ; list -> size -- ; return element ; } else if ( index == list -> size - 1 ) { memcpy ( element , list -> last -> data , list -> itemSize ) ; list -> last = list -> last -> prev ; list -> last -> next = NULL ; list -> size -- ; return element ; } else { Node * temp = list -> first ; for ( int i = 0 ; i < index ; i ++ ) temp = temp -> next ; memcpy ( element , temp -> data , list -> itemSize ) ; temp -> prev -> next = temp -> next ; temp -> next -> prev = temp -> prev ; list -> size -- ; return element ; } }
The linked_list_remove function removes an element fron the linledlist struct
 at a specific index.
The linked_list_remove function removes an element fron the linledlist struct at a specific index .
// If the list is NULL or if the index is out of bounds, return immediately. // Allocate memory for the data value to be returned. // If the list contains only 1 element. // Copy the data from first element, make first and last references NULL. // Decrement the size of the list and return the element. // Else if the list is greater than 1 element but the index is 0. // Copy the data from first into the element variable. // Make first now point to the second element in the list. // Else if the index is the last element in the list. // Copy the data from last into the element. // Make the new last reference the second to last element. // Else the element to remove is somewhere between the first and last element of the list. // Creating a local temporary Node reference to walk the list. // For each Node in the list less that the index, walk Node reference through the list. // Copy the data value into the element variable. // Adjust next and prev references to remove the temp node reference from the list.