list_remove
This commit is contained in:
parent
69ae9eea47
commit
e525a5e557
1 changed files with 37 additions and 0 deletions
|
@ -49,6 +49,21 @@ void list_add(list_node_t *list, list_node_t *item) {
|
|||
cur->next = item;
|
||||
}
|
||||
|
||||
void list_remove(list_node_t *list, list_node_t *item) {
|
||||
list_node_t *cur;
|
||||
|
||||
cur = list,
|
||||
assert(cur != NULL);
|
||||
|
||||
while (cur->next != NULL) {
|
||||
if (cur->next->data == item->data) {
|
||||
cur->next = cur->next->next;
|
||||
return; /* just the first */
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
list_node_t *emptylist = NULL;
|
||||
assert(list_length(emptylist) == 0);
|
||||
|
@ -83,4 +98,26 @@ int main(int argc, char *argv[]) {
|
|||
assert(list_length(&foo) == 2 + howmany);
|
||||
list_print(&foo);
|
||||
printf("list_length = %d\n", list_length(&foo));
|
||||
|
||||
// remove something
|
||||
list_node_t toremove = { 23, NULL };
|
||||
list_remove(&foo, &toremove);
|
||||
assert(list_length(&foo) == 2 + howmany - 1);
|
||||
list_print(&foo);
|
||||
printf("list_length = %d\n", list_length(&foo));
|
||||
|
||||
// remove something
|
||||
toremove.data = 99;
|
||||
list_remove(&foo, &toremove);
|
||||
assert(list_length(&foo) == 2 + howmany - 2);
|
||||
list_print(&foo);
|
||||
printf("list_length = %d\n", list_length(&foo));
|
||||
|
||||
// remove something
|
||||
toremove.data = 1;
|
||||
list_remove(&foo, &toremove);
|
||||
/* FIXME can't delete first 1 */
|
||||
assert(list_length(&foo) == 2 + howmany - 3);
|
||||
list_print(&foo);
|
||||
printf("list_length = %d\n", list_length(&foo));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue