fixed very old bug in linked_list's remove_first and remove_last

This commit is contained in:
Martin Willi
2006-11-02 14:25:58 +00:00
parent 116b53b6bd
commit 3b449ce854
+17 -9
View File
@@ -392,12 +392,12 @@ static void insert_first(private_linked_list_t *this, void *item)
*/ */
static status_t remove_first(private_linked_list_t *this, void **item) static status_t remove_first(private_linked_list_t *this, void **item)
{ {
if (this->count == 0) element_t *element = this->first;
if (element == NULL)
{ {
return NOT_FOUND; return NOT_FOUND;
} }
element_t *element = this->first;
if (element->next != NULL) if (element->next != NULL)
{ {
element->next->previous = NULL; element->next->previous = NULL;
@@ -408,8 +408,13 @@ static status_t remove_first(private_linked_list_t *this, void **item)
{ {
*item = element->value; *item = element->value;
} }
this->count--; if (--this->count == 0)
{
this->last = NULL;
}
free(element); free(element);
return SUCCESS; return SUCCESS;
} }
@@ -457,13 +462,12 @@ static void insert_last(private_linked_list_t *this, void *item)
*/ */
static status_t remove_last(private_linked_list_t *this, void **item) static status_t remove_last(private_linked_list_t *this, void **item)
{ {
if (this->count == 0) element_t *element = this->last;
if (element == NULL)
{ {
return NOT_FOUND; return NOT_FOUND;
} }
element_t *element = this->last;
if (element->previous != NULL) if (element->previous != NULL)
{ {
element->previous->next = NULL; element->previous->next = NULL;
@@ -474,9 +478,13 @@ static status_t remove_last(private_linked_list_t *this, void **item)
{ {
*item = element->value; *item = element->value;
} }
if (--this->count == 0)
{
this->first = NULL;
}
this->count--;
free(element); free(element);
return SUCCESS; return SUCCESS;
} }