plibsys 0.0.5
|
Singly linked list. More...
Go to the source code of this file.
Data Structures | |
struct | PList_ |
Node for a singly linked list. More... | |
Typedefs | |
typedef struct PList_ | PList |
Typedef for a list node. | |
Functions | |
P_LIB_API PList * | p_list_append (PList *list, ppointer data) P_GNUC_WARN_UNUSED_RESULT |
Appends data to a list. | |
P_LIB_API PList * | p_list_remove (PList *list, pconstpointer data) P_GNUC_WARN_UNUSED_RESULT |
Removes data from a list. | |
P_LIB_API void | p_list_foreach (PList *list, PFunc func, ppointer user_data) |
Calls a specified function for each list node. | |
P_LIB_API void | p_list_free (PList *list) |
Frees list memory. | |
P_LIB_API PList * | p_list_last (PList *list) |
Gets the last node from the list. | |
P_LIB_API psize | p_list_length (const PList *list) |
Gets the number of list nodes. | |
P_LIB_API PList * | p_list_prepend (PList *list, ppointer data) P_GNUC_WARN_UNUSED_RESULT |
Prepends data to a list. | |
P_LIB_API PList * | p_list_reverse (PList *list) P_GNUC_WARN_UNUSED_RESULT |
Reverses the list order. | |
Singly linked list.
A singly linked list is a data structure consists of the nodes which represent a sequence. Each node contains a data pointer and a pointer to the next node. Every node has a link only to the next node, hence list is a singly linked (in a single direction).
As the singly linked list is a linear collection of the nodes with the sequential access, it has an O(N) average complexity for appending, removing and searching operations. Prepending a node takes O(1) constant time. Thus it is not intended for heavy usage, please refer to PHashTable or PTree if you are working with large data sets.
Before the first usage you must initialize a PList variable to NULL. After that you can use the p_list_append(), p_list_prepend(), p_list_remove() and p_list_reverse() routines to update that variable:
PList stores only the pointers to the data, so you must free used memory manually, p_list_free() only frees list's internal memory, not the data it stores the pointers for. The best approach to free used memory is the p_list_foreach() routine:
Also you can use P_INT_TO_POINTER and P_POINTER_TO_INT macros to store integers (up to 32-bit) without allocating memory for them:
PList can store several nodes with the same pointer value, but p_list_remove() will remove only the first matching node.
If you need to add large amount of nodes at once it is better to prepend them and then reverse the list.
Definition in file plist.h.
Appends data to a list.
list | PList for appending the data. |
data | Data to append. |
Before appending the first node to the list, list argument must be initialized with NULL. Otherwise behavior is unpredictable.
Calls a specified function for each list node.
list | List to go through. |
func | Pointer for the callback function. |
user_data | User defined data, may be NULL. |
This function goes through the whole list and calls func for each node. The func will receive pointer to the node's data and user_data. You can use it to free the data:
Frees list memory.
list | List to free. |
This function frees only the list's internal memory, not the data in the pointers stored in the nodes. Don't forget to free all the data stored in the list manually.
Gets the last node from the list.
list | List to get the node from. |
Gets the number of list nodes.
list | List to count nodes in. |
Prepends data to a list.
list | PList for prepending the data. |
data | Data to prepend. |
Before prepending the first node to the list, list argument must be initialized with NULL. Otherwise behavior is unpredictable.
P_LIB_API PList * p_list_remove | ( | PList * | list, |
pconstpointer | data ) |
Removes data from a list.
list | List to remove the data from. |
data | Data to remove. |
It searches for the first matching occurrence in the list and removes that node. Note that it removes only the pointer from the list, not the data it pointers to, so you need to free the data manually.