#include #include "list.h" /* * Print the size and the contents of a list. */ void List_dump(List_T list) { List_iterator_T p; printf("%d\n", List_size(list)); for (p = List_begin(list); p != List_end(list); p = List_next(p)) { printf("%s\n", List_element(list, p)); } } int main(int argc, char *argv[]) { List_T list; List_iterator_T p; int i; list = List_new(); /* * Insert the command line arguments in the list. Each argument is * inserted in the END of the list. */ for (i = 0; i < argc; i++) { List_insert(list, List_end(list), argv[i]); } /* * Remember where the beginning of the list was before the next * insertions. */ p = List_begin(list); /* * Insert the command line arguments in the list again. This time * each argument is inserted in the BEGINING of the list. */ for (i = 0; i < argc; i++) { List_insert(list, List_begin(list), argv[i]); } /* * Insert a separator between the two copies of the arguments. */ List_insert(list, p, "Separator"); p = List_previous(p); /* Remember the position of the separator. */ List_dump(list); /* * Erase the separator. */ printf("After erasing %s\n", List_erase(list, p)); List_dump(list); /* * Traverse the first half of the list erasing the elements from the * BEGINNING of the list. */ for (i = 0; i < argc; i++) { printf("Erasing %s\n", List_erase(list, List_begin(list))); } List_dump(list); /* * Traverse the rest of the list erasing the elements from the END * of the list. Notice that the element erased is the first one * before the one pointed to by List_end(), since List_end() points * to the first *unused* node. */ while (!List_is_empty(list)) { printf("Erasing: %s\n", List_erase(list, List_previous(List_end(list)))); } List_dump(list); List_delete(&list); return 0; }