List functions implementation.
More...
#include "list.h"
#include "pool.h"
#include <stdbool.h>
Go to the source code of this file.
|
| static void | s_list_remove_node (List *list, ListNode *node) |
| |
| static ListNode * | s_list_itr_node_next (ListItr *itr) |
| |
| List | list_init (void) |
| |
| void | list_clear (List *list) |
| |
| bool | list_is_empty (const List *list) |
| |
| void | list_push_front (List *list, void *data) |
| |
| void | list_push_back (List *list, void *data) |
| |
| void | list_insert (List *list, void *data, unsigned int idx) |
| |
| bool | list_swap (List *list, unsigned int idx_a, unsigned int idx_b) |
| |
| int | list_get_len (const List *list) |
| |
| void * | list_get_at_idx (List *list, unsigned int idx) |
| |
| bool | list_remove_at_idx (List *list, unsigned int idx) |
| |
| ListItr | list_itr_create (List *list) |
| |
| ListItr | rev_list_itr_create (List *list) |
| |
| void * | list_itr_next (ListItr *itr) |
| |
| void | list_itr_remove_current_node (ListItr *itr) |
| |
| bool | list_remove_data (List *list, void *data) |
| |
List functions implementation.
Definition in file list.c.
◆ list_clear()
| void list_clear |
( |
List * |
list | ) |
|
Clear a list.
Go through the list and free each node and set the head and tail to NULL. Note, it doesn't "free" the data at the node.
- Note
- To reset an existing list to default values, first call
list_clear then list_init
- Parameters
-
| list | pointer to a List to clear |
Definition at line 44 of file list.c.
◆ list_get_at_idx()
| void * list_get_at_idx |
( |
List * |
list, |
|
|
unsigned int |
idx |
|
) |
| |
Get a List's node at the specified index
- Parameters
-
| list | pointer to a List |
| idx | index of the desired ListNode in the list |
- Returns
- a pointer to the data at the index of the list, or NULL if out-of-bounds
Definition at line 219 of file list.c.
◆ list_get_len()
| int list_get_len |
( |
const List * |
list | ) |
|
Get the number of elements in a List
- Parameters
-
- Returns
- The number of elements in the list
Definition at line 214 of file list.c.
◆ list_init()
Initialize a list.
Set the values of a list to default.
If using this function to reset a list, the list must be freed with list_clear to ensure the list's nodes are deleted properly.
- Returns
- A List with head and tail reset.
Definition at line 38 of file list.c.
◆ list_insert()
| void list_insert |
( |
List * |
list, |
|
|
void * |
data, |
|
|
unsigned int |
idx |
|
) |
| |
Insert data into a List a specific index
If the index specified is larger than the length of the list it will list_push_back() the data instead;
Performs the following operation:
┌─────┐
│ node│
└─────┘
┌─────┐ ┌─────┐ ┌─────┐
│idx-1│◄─►│ idx │◄─►│idx+1│
└─────┘ └─────┘ └─────┘
- Set new
node prev to the node at idx - 1
- Set new
node next to the node at idx
- Set node at idx - 1
next to new node
- Set node at idx
prev to the new node
Result:
┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
│idx-1│◄─►│ node│◄─►│ idx │◄─►│idx+1│
└─────┘ └─────┘ └─────┘ └─────┘
Finally, the list is now updated with new node now at the labeled idx:
┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
│idx-1│◄─►│ idx │◄─►│idx+1│◄─►│idx+2│
└─────┘ └─────┘ └─────┘ └─────┘
- Parameters
-
| list | pointer to a List |
| data | pointer to data to put into the List |
| idx | desired index to insert |
Definition at line 110 of file list.c.
◆ list_is_empty()
| bool list_is_empty |
( |
const List * |
list | ) |
|
Check if a list is empty
- Parameters
-
- Returns
true if the list is empty, false otherwise.
Definition at line 62 of file list.c.
◆ list_itr_create()
◆ list_itr_next()
| void * list_itr_next |
( |
ListItr * |
itr | ) |
|
Get the next data entry in a ListItr
- Parameters
-
- Returns
- A pointer to the data pointer at the next ListNode if valid, otherwise return NULL.
Definition at line 281 of file list.c.
◆ list_itr_remove_current_node()
| void list_itr_remove_current_node |
( |
ListItr * |
itr | ) |
|
Remove the current ListNode from the iterator.
The "current node" corresponds to the list node associated with the most recently returned valu from list_itr_next()
- Parameters
-
- Note
- When working with ListItr, use this and not list_remove_at() as it will "break" the iterator.
Definition at line 307 of file list.c.
◆ list_push_back()
| void list_push_back |
( |
List * |
list, |
|
|
void * |
data |
|
) |
| |
Append an entry to the tail of a list
- Parameters
-
| list | pointer to a List |
| data | pointer to data to put into the List |
Definition at line 89 of file list.c.
◆ list_push_front()
| void list_push_front |
( |
List * |
list, |
|
|
void * |
data |
|
) |
| |
Prepend an entry to the head of a list
- Parameters
-
| list | pointer to a List |
| data | pointer to data to put into the List |
Definition at line 67 of file list.c.
◆ list_remove_at_idx()
| bool list_remove_at_idx |
( |
List * |
list, |
|
|
unsigned int |
idx |
|
) |
| |
Remove a List's node at the specified index
- Parameters
-
| list | pointer to a List |
| idx | index of the desired ListNode in the list |
- Returns
true if successfully removed, false if out-of-bounds
Definition at line 237 of file list.c.
◆ list_remove_data()
| bool list_remove_data |
( |
List * |
list, |
|
|
void * |
data |
|
) |
| |
Remove a List's node with the matching pointer
- Parameters
-
| list | pointer to a List |
| data | pointer to data in node in list |
- Returns
true if successfully removed, false otherwise
- Note
- When working with ListItr, use list_itr_remove_current_node()
Definition at line 316 of file list.c.
◆ list_swap()
| bool list_swap |
( |
List * |
list, |
|
|
unsigned int |
idx_a, |
|
|
unsigned int |
idx_b |
|
) |
| |
Swap the data pointers at the specified indices of a List
If either indices are larger than the length of the list, return false.
- Parameters
-
| list | pointer to a List |
| idx_a | desired index to swap with idx_b |
| idx_b | desired index to swap with idx_a |
- Returns
- true if successful, false otherwise
Definition at line 148 of file list.c.
◆ rev_list_itr_create()
◆ s_list_itr_node_next()
Get the next ListNode in a ListItr
Note: Use of this function outside of testing is strongly discouraged. Unless you really want to access the ListNode itself, it's preferred to just use list_itr_next .
- Parameters
-
- Returns
- A pointer to the ListNode in the itr, otherwise return NULL.
Definition at line 287 of file list.c.
◆ s_list_remove_node()
| static void s_list_remove_node |
( |
List * |
list, |
|
|
ListNode * |
node |
|
) |
| |
|
static |
Remove a node from a list.
Remove a ListNode from a List. There are no checks to ensure that the passed node is actually part of the passed list. Handle with care. This is used with the ListItr specifically.
- Parameters
-
Definition at line 186 of file list.c.