int dlist_ins_next(DList *list, DListElmt *element, ListData *data){
if (element == NULL && dlist_size(list) != 0) return -1;
//no null elements
if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1;
//allocate storage
new_element->data = (void *)data; //insert new element into list
if (dlist_size(list) == 0) { //if list is empty
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
}
else{ //not empty list
new_element->next = element->next;
new element->prev = element;
if(element->next == NULL)
list->tail = new_element;
else{
element->next->prev = new_element;}
element->next = new_element;
}//end of else
list->size++;
return 0;
}
|