cat test.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
gcc -std=c99 test.c
./a.out
0
1
2
3
4
5
6
7
8
9
// clist holds CHAR_DATA *
CHAR_LIST clist;
init_char_list(&clist); // initialize our list.
while ( we have data to put in list) {
ch = cast_data();
char_to_list(&clist, ch); // defaults to push_front (can also call char_to_list_back() to push_back)
}
// so we have a list with a certain number of elements in it populated.
// Iterate over the list and print each "name" field from the char_data in the list.
for(ITERATOR it = get_char_iterator(&clist);it;it = next_char_iterator(it))
printf("%s ", ref_char_iterator(it)->name); // prints name.
// You can "peek" at any element without popping it from the list with this function
peek_char_list(&clist, 1); // returns the CHAR_DATA * at the first position.
peek_char_list_back(&clist, 1); // returns the CHAR_DATA * at the first position from the back of the list.
// Note, these wouldn't remove it.
// Now let's pop the top element from the list and print its name.
if (count_char_list(&clist) > 1) // only if the list actually contains anything.
printf("%s ", pop_char_list(&clist)->name); // removes the first node and returns the data it had.
// If we have nodes left then let's iterate through them, freeing them, and preserving the correct list.
for(ITERATOR it = get_char_iterator(&clist);it;it = next_char_iterator(it)) {
printf("%s ", ref_char_iterator(it)->name); // prints name;
remove_char_list(&clist, it); // Remove the element at it, doesn't matter what gets freed or removed.
// Continue on even though this node isn't valid any more.
}
free_char_list(&clist); // clean up the list if it's being destroyed.