libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
slist.h File Reference

Singly-linked list. More...

#include "datatypes.h"
#include <stdint.h>
#include <stdbool.h>
#include "pool.h"

Go to the source code of this file.

Data Structures

struct  SListItem
 Structure holding a list item. More...
 
struct  SList
 Singly-linked list. More...
 

Macros

#define slist_item_next(item)   item->next
 
#define slist_item_get_data(item)   item->data
 
#define slist_item_set_data(item, value)   item->data = value
 

Functions

SListslist_new (CompareFunc compare, FreeFunc free, Pool *pool)
 
void slist_init (SList *list, CompareFunc compare, FreeFunc free, Pool *pool)
 
void slist_destroy (SList *list)
 
void slist_free (SList *list)
 
SListItemslist_append (SList *list, void *data)
 
SListItemslist_prepend (SList *list, void *data)
 
SListItemslist_insert_sorted (SList *list, void *data)
 
SListItemslist_head (const SList *list)
 
size_t slist_count (const SList *list)
 
bool slist_empty (const SList *list)
 
void slist_remove (SList *list, SListItem *item)
 
void slist_remove_by_data (SList *list, void *data, bool remove_all)
 
void * slist_pop (SList *list)
 
bool slist_contains (const SList *list, const void *data)
 
void slist_clear (SList *list)
 
SListItemslist_find (const SList *list, SListItem *offset, void const *data)
 
void slist_item_free_data (const SList *list, SListItem *item)
 

Detailed Description

Singly-linked list.

Author
Sebastian Fedrau sebas.nosp@m.tian.nosp@m..fedr.nosp@m.au@g.nosp@m.mail..nosp@m.com

Definition in file slist.h.


Data Structure Documentation

◆ SListItem

struct SListItem

Structure holding a list item.

Definition at line 36 of file slist.h.

Data Fields
void * data

Data of the list item.

struct _SListItem * next

Pointer to next list element or NULL.

◆ SList

struct SList

Singly-linked list.

Definition at line 48 of file slist.h.

Data Fields
CompareFunc compare

Function to compare data of two list items.

size_t count

Number of stored items.

FreeFunc free

Function to free item data.

SListItem * head

Head of the list.

Pool * pool

A memory pool for creating new list items.

SListItem * tail

Tail of the list.

Macro Definition Documentation

◆ slist_item_get_data

#define slist_item_get_data (   item)    item->data

Gets data of the specified SListItem.

Definition at line 204 of file slist.h.

◆ slist_item_next

#define slist_item_next (   item)    item->next

Get next list item.

Definition at line 201 of file slist.h.

◆ slist_item_set_data

#define slist_item_set_data (   item,
  value 
)    item->data = value

Sets data of the specified SListItem.

Definition at line 207 of file slist.h.

Function Documentation

◆ slist_append()

SListItem * slist_append ( SList list,
void *  data 
)
Parameters
lista SList
datadata to append
Returns
a new SListItem

Appends data to the list.

Definition at line 133 of file slist.c.

◆ slist_clear()

void slist_clear ( SList list)
Parameters
lista SList

Clears a list.

Definition at line 390 of file slist.c.

◆ slist_contains()

bool slist_contains ( const SList list,
const void *  data 
)
Parameters
lista SList
datadata to search
Returns
true if data exists in list

Tests if a list contains the specified data.

Definition at line 381 of file slist.c.

◆ slist_count()

size_t slist_count ( const SList list)
Parameters
lista SList
Returns
number of items

Gets the number of items.

Definition at line 410 of file slist.c.

◆ slist_destroy()

void slist_destroy ( SList list)
Parameters
lista SList

Frees all items in the list and the list pointer.

Definition at line 98 of file slist.c.

◆ slist_empty()

bool slist_empty ( const SList list)
Parameters
lista SList
Returns
true if list is empty

Checks if a list is empty.

Definition at line 426 of file slist.c.

◆ slist_find()

SListItem * slist_find ( const SList list,
SListItem offset,
void const *  data 
)
Parameters
lista SList
offsetposition to start search from
datadata to search
Returns
found SListItem or NULL

Searches for an element in the list.

Definition at line 402 of file slist.c.

◆ slist_free()

void slist_free ( SList list)
Parameters
lista SList

Frees all items in the list without freeing the list pointer.

Definition at line 81 of file slist.c.

◆ slist_head()

SListItem * slist_head ( const SList list)
Parameters
lista SList
Returns
head of the list

Gets the head of the list.

Definition at line 418 of file slist.c.

◆ slist_init()

void slist_init ( SList list,
CompareFunc  compare,
FreeFunc  free,
Pool pool 
)
Parameters
lista SList
comparefunction to compare item data
freefunction to free item data OR NULL
poola user-defined memory pool for creating/destroying SListItems or NULL

Initializes a new SList.

Definition at line 48 of file slist.c.

◆ slist_insert_sorted()

SListItem * slist_insert_sorted ( SList list,
void *  data 
)
Parameters
lista List
datadata to insert
Returns
a new ListItem

Inserts data into the list using the associated compare function to determine its position.

Definition at line 219 of file slist.c.

◆ slist_item_free_data()

void slist_item_free_data ( const SList list,
SListItem item 
)
Parameters
lista SList
itema SListItem

Sets the associated list item to NULL and frees memory.

Definition at line 434 of file slist.c.

◆ slist_new()

SList * slist_new ( CompareFunc  compare,
FreeFunc  free,
Pool pool 
)
Parameters
comparefunction to compare item data
freefunction to free item data or NULL
poola user-defined memory pool for creating/destroying SListItems or NULL
Returns
a new SList

Creates a new SList.

Definition at line 30 of file slist.c.

◆ slist_pop()

void * slist_pop ( SList list)
Parameters
lista SList
Returns
list item data

Removes first element from list and returns data.

Definition at line 325 of file slist.c.

◆ slist_prepend()

SListItem * slist_prepend ( SList list,
void *  data 
)
Parameters
lista SList
datadata to prepend
Returns
a new SListItem

Prepends data to the list.

Definition at line 162 of file slist.c.

◆ slist_remove()

void slist_remove ( SList list,
SListItem item 
)
Parameters
lista SList
itema SListItem

Removes a list item from the list.

Definition at line 254 of file slist.c.

◆ slist_remove_by_data()

void slist_remove_by_data ( SList list,
void *  data,
bool  remove_all 
)
Parameters
lista SList
datadata
remove_alltrue to remove all items with associated data

Removes first list item (or all items) with associated data from the list.

Definition at line 289 of file slist.c.