#include #include #include #include "assert.h" struct list_node { int data; struct list_node *next; }; typedef struct list_node list_node_t; struct list { list_node_t * head; }; typedef struct list list_t; void list_init (list_t * list) { list->head = NULL; } int list_length(list_t * list) { list_node_t *cur; int count = 0; cur = list->head; while (cur != NULL) { count++; cur = cur->next; } return count; } bool list_empty(list_t * list) { return (list->head == NULL); } void list_print(list_t * list) { list_node_t *cur; cur = list->head; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } printf("\n"); } void list_add(list_t * list, list_node_t * item) { list_node_t *cur; if (list_empty(list)) { list->head = item; } else { cur = list->head; while (cur->next != NULL) { cur = cur->next; } cur->next = item; } } void list_remove(list_t * list, list_node_t * item) { list_node_t *cur; if (list->head->data == item->data) { list->head = list->head->next; return; } cur = list->head; 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; } } /* FIXME * list_node_t list_revert(list_node_t *list) { } */ int main(int argc, char *argv[]) { list_t *emptylist = malloc(sizeof(list_t)); list_init(emptylist); assert(list_length(emptylist) == 0); assert(list_empty(emptylist) == true); list_print(emptylist); printf("list_length = %d\n", list_length(emptylist)); list_t *foolist = malloc(sizeof(list_t)); list_node_t foo = { 1, NULL }; list_add(foolist, &foo); assert(list_length(foolist) == 1); assert(list_empty(foolist) == false); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // append something to foo list_node_t bar = { 2, NULL }; list_add(foolist, &bar); assert(list_length(foolist) == 2); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // append more const int howmany = 100; for (int i = 0; i < howmany; i++) { list_node_t *newnode = malloc(sizeof(list_node_t)); newnode->data = i; list_add(foolist, newnode); } assert(list_length(foolist) == 2 + howmany); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // remove something list_node_t toremove = { 23, NULL }; list_remove(foolist, &toremove); assert(list_length(foolist) == 2 + howmany - 1); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // remove last item toremove.data = howmany - 1; list_remove(foolist, &toremove); assert(list_length(foolist) == 2 + howmany - 2); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // remove something toremove.data = 1; list_remove(foolist, &toremove); assert(list_length(foolist) == 2 + howmany - 3); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); // remove something toremove.data = 1; list_remove(foolist, &toremove); assert(list_length(foolist) == 2 + howmany - 4); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); /* remove everything */ for (int i = 0; i < howmany; i++) { toremove.data = i; list_remove(foolist, &toremove); } assert(list_length(foolist) == 1); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); /* remove last remaining item */ toremove.data = 2; list_remove(foolist, &toremove); assert(list_length(foolist) == 0); assert(list_empty(foolist) == true); list_print(foolist); printf("list_length = %d\n", list_length(foolist)); }