双向链表
typedef struct DListElement_
{
int data;
struct DListElement_ *prev;
struct DListElement_ *next;
}DListElement;
typedef struct DList_
{
int size;
DListElement *head;
DListElement *tail;
}DList;
int dlist_ins_prev(DList* list, DListElement *element, const int data)
{
DListElement * new_element = (DListElement*)malloc(sizeof(DListElement));
if (NULL == list || NULL == new_element || NULL == element)
{
return -1;
}
new_element->data = data;
if (list->size == 0)
{
list->head = new_element;
list->tail = new_element;
new_element->next = NULL;
new_element->prev = NULL;
}
else
{
if (NULL == element->prev)
{
new_element->prev = NULL;
new_element->next = element;
element->prev = new_element;
}
else
{
new_element->prev = element->prev;
new_element->next = element;
element->prev->next = new_element;
element->prev = new_element;
}
}
list->size++;
return 0;
}
int dlist_ins_next(DList* list, DListElement *element, const int data)
{
DListElement * new_element = (DListElement*)malloc(sizeof(DListElement));
if (NULL == list || NULL == new_element || NULL == element)
{
return -1;
}
new_element->data = data;
if (list->size == 0)
{
list->tail = new_element;
list->head = new_element;
new_element->next = NULL;
new_element->prev = NULL;
}
else
{
if (NULL == element->next)
{
list->tail = new_element;
element->next = new_element;
new_element->prev = element;
new_element->next = NULL;
}
else
{
new_element->next = element->next;
new_element->prev = element;
element->next->prev = new_element;
element->next = new_element;
}
}
list->size++;
return 0;
}
int deleElement(DList* list, DListElement* element)
{
if (NULL == list || NULL == element)
{
return -1;
}
if (NULL == element->prev)
{
list->head = element->next;
list->head->prev = NULL;
}
if (NULL == element->next)
{
list->tail = element->prev;
element->prev->next = NULL;
}
else
{
element->next->prev = element->prev;
element->prev->next = element->next;
}
if (NULL != element)
{
free(element);
}
list->size--;
return 0;
}