Next: List of int32_t, Previous: Linked lists, Up: Linked lists [Contents][Index]
Probably one of the most common lists you will be using are lists of strings. They are the best tools when you are reading the user’s inputs, or when adding comments to the output files. Below you can see Gnuastro’s string list type and several functions to help in adding, removing/popping, reversing and freeing the list.
struct
): gal_list_str_tA single node in a list containing a string of characters.
typedef struct gal_list_str_t { char *v; struct gal_list_str_t *next; } gal_list_str_t;
**list
, char *value
, int allocate
)Add a new node to the list of strings (list
) and update it. The new
node will contain the string value
. If allocate
is not zero,
space will be allocated specifically for the string of the new node and the
contents of value
will be copied into it. This can be useful when
your string may be changed later in the program, but you want your list to
remain. Here is one short/simple example of initializing and adding
elements to a string list:
gal_list_str_t *strlist=NULL; gal_list_str_add(&strlist, "bottom of list."); gal_list_str_add(&strlist, "second last element of list.");
**list
)Pop the top element of list
, change list
to point to the next
node in the list, and return the string that was in the popped node. If
*list==NULL
, then this function will also return a NULL
pointer.
*list
)Return the number of nodes in list
.
*list
)Return a pointer to the last node in list
.
*list
)Print the strings within each node of *list
on the standard output
in the same order that they are stored. Each string is printed on one
line. This function is mainly good for checking/debugging your program. For
program outputs, its best to make your own implementation with a better,
more user-friendly, format. For example the following code snippet.
size_t i; gal_list_str_t *tmp; for(tmp=list; tmp!=NULL; tmp=tmp->next) printf("String %zu: %s\n", i, tmp->v);
**list
)Reverse the order of the list such that the top node in the list before calling this function becomes the bottom node after it.
*list
, int freevalue
)Free every node in list
. If freevalue
is not zero, also free
the string within the nodes.
Next: List of int32_t, Previous: Linked lists, Up: Linked lists [Contents][Index]
GNU Astronomy Utilities 0.13 manual, September 2020.