Piconomic Logo www.piconomic.co.za

list.h : Linked List
[/general : General utility files & modules]

A doubly (forwards and backwards) linked list using pointers. More...

Data Structures

struct  list_item_t
 Link structure that must be at the head of each item in the list. More...
struct  list_t
 Linked list structure. More...

Functions

void list_init (list_t *list, size_t max_nr_of_items)
 Initialises a linked list structure.
void list_item_init (list_t *list, list_item_t *item)
 Initialises a list item.
bool_t list_is_empty (list_t *list)
 See if the list is empty.
bool_t list_is_full (list_t *list)
 See if the list is full.
size_t list_nr_of_items (list_t *list)
 Get the number of items in the list.
list_item_tlist_first_item (list_t *list)
 Get a pointer to the first item in the list.
list_item_tlist_last_item (list_t *list)
 Get a pointer to the last item in the list.
list_item_tlist_next_item (list_t *list, list_item_t *item)
 Get a pointer to the next item in the list (after the specified item).
list_item_tlist_previous_item (list_t *list, list_item_t *item)
 Get a pointer to the previous item in the list (before the specified item).
bool_t list_add_to_start (list_t *list, list_item_t *item)
 Insert item to the start of the list.
bool_t list_add_to_end (list_t *list, list_item_t *item)
 Add item to the end of the list.
list_item_tlist_remove_first_item (list_t *list)
 Remove first item from the list.
list_item_tlist_remove_last_item (list_t *list)
 Remove last item from the list.
void list_remove_item (list_t *list, list_item_t *item)
 Remove item from the list.
bool_t list_item_in_list (list_t *list, list_item_t *item)
 See if item is in the list.

Detailed Description

Files: list.h & list.c

See also:
http://en.wikipedia.org/wiki/Linked_list

Graphical example:

  No items in list:
 
  [first = NULL] [last = NULL]
 
  One item in list:
 
  [first] -> [prev = NULL][next = NULL] <- [last]
 
  Two items in list:
 
  [first] -> [prev = NULL][next] <-> [prev][next = NULL]  <- [last]
 
  Three items in list:
 
  [first] -> [prev = NULL][next] <-> [prev][next] <-> [prev][next = NULL]  <- [last]

Function Documentation

void list_init ( list_t list,
size_t  max_nr_of_items 
)
Parameters:
list Pointer to the linked list
max_nr_of_items Maximum number of items allowed in list; 0 means no limit

Definition at line 58 of file list.c.

References list_t::first_item, list_t::last_item, list_t::max_nr_of_items, list_t::nr_of_items, and NULL.

00060 {
00061     list->first_item      = NULL;
00062     list->last_item       = NULL;
00063     list->nr_of_items     = 0;
00064     list->max_nr_of_items = max_nr_of_items;
00065 }

void list_item_init ( list_t list,
list_item_t item 
)

Initialises the item structure to indicate that it is not in the list.

See also:
list_item_in_list()
Parameters:
list Pointer to the linked list
item Pointer to specified item

Definition at line 67 of file list.c.

References NULL.

00069 {
00070     item->previous_item = NULL;
00071     item->next_item     = NULL;
00072 }

bool_t list_is_empty ( list_t list  ) 
Parameters:
list Pointer to the linked list
Returns:
TRUE List is empty
FALSE List contains one or more items

Definition at line 74 of file list.c.

References list_t::first_item, and NULL.

Referenced by list_add_to_end(), list_add_to_start(), list_remove_first_item(), list_remove_item(), and list_remove_last_item().

00075 {
00076     if(list->first_item == NULL)
00077     {
00078         return TRUE;
00079     }
00080     else
00081     {
00082         return FALSE;
00083     }
00084 }

bool_t list_is_full ( list_t list  ) 
Parameters:
list Pointer to the linked list
Return values:
TRUE The list is full
FALSE The list is not full, or there is no limit (max_nr_of_items = 0)

Definition at line 86 of file list.c.

References list_t::max_nr_of_items, and list_t::nr_of_items.

Referenced by list_add_to_end(), and list_add_to_start().

00087 {
00088     if(list->max_nr_of_items == 0)
00089     {
00090         return FALSE;
00091     }
00092 
00093     if(list->nr_of_items < list->max_nr_of_items)
00094     {
00095         return FALSE;
00096     }
00097     else
00098     {
00099         return TRUE;
00100     }
00101 }

size_t list_nr_of_items ( list_t list  ) 
Parameters:
list Pointer to the linked list
Returns:
size_t The number of items in the list

Definition at line 103 of file list.c.

References list_t::nr_of_items.

00104 {
00105     // Return item count
00106     return (list->nr_of_items);
00107 }

list_item_t* list_first_item ( list_t list  ) 
Parameters:
list Pointer to the linked list
Returns:
list_item_t* Pointer to the first item in the list; NULL will be returned if the list is empty.

Definition at line 109 of file list.c.

References list_t::first_item.

00110 {
00111     return list->first_item;
00112 }

list_item_t* list_last_item ( list_t list  ) 

param list Pointer to the linked list

Returns:
list_item_t* Pointer to the last item in the list; NULL will be returned if the list is empty.

Definition at line 114 of file list.c.

References list_t::last_item.

00115 {
00116     return list->last_item;
00117 }

list_item_t* list_next_item ( list_t list,
list_item_t item 
)

param list Pointer to the linked list

Returns:
list_item_t* Pointer to the next item in the list; NULL will be returned if the specified item is the last one in the list.

Definition at line 119 of file list.c.

00121 {
00122     return item->next_item;
00123 }

list_item_t* list_previous_item ( list_t list,
list_item_t item 
)

* param list Pointer to the linked list

Returns:
list_item_t* Pointer to the next item in the list; NULL will be returned if the specified item is the first one in the list.

Definition at line 125 of file list.c.

00127 {
00128     return item->previous_item;
00129 }

bool_t list_add_to_start ( list_t list,
list_item_t item 
)
Parameters:
list Pointer to the linked list
item Item to be inserted
Return values:
TRUE Item has been inserted
FALSE List is full

Definition at line 131 of file list.c.

References list_t::first_item, list_t::last_item, list_is_empty(), list_is_full(), list_t::nr_of_items, and NULL.

00133 {
00134     if(list_is_full(list))
00135     {
00136         return FALSE;
00137     }
00138 
00139     if(list_is_empty(list))
00140     {
00141         // Add first item
00142         list->first_item    = item;
00143         list->last_item     = item;
00144         item->next_item     = NULL;
00145         item->previous_item = NULL;
00146     }
00147     else
00148     {
00149         // Insert new item before first item
00150         item->previous_item             = NULL;
00151         item->next_item                 = list->first_item;
00152         list->first_item->previous_item = item;
00153         list->first_item                = item;
00154     }
00155 
00156     // Increment item count
00157     (list->nr_of_items)++;
00158 
00159     return TRUE;
00160 }

bool_t list_add_to_end ( list_t list,
list_item_t item 
)
Parameters:
list Pointer to the linked list
item Item to be inserted
Return values:
TRUE Item has been inserted
FALSE List is full

Definition at line 162 of file list.c.

References list_t::first_item, list_t::last_item, list_is_empty(), list_is_full(), list_t::nr_of_items, and NULL.

00164 {
00165     if(list_is_full(list))
00166     {
00167         return FALSE;
00168     }
00169 
00170     if(list_is_empty(list))
00171     {
00172         // Add first item
00173         list->first_item    = item;
00174         list->last_item     = item;
00175         item->next_item     = NULL;
00176         item->previous_item = NULL;
00177     }
00178     else
00179     {
00180         // Append new item to last item
00181         item->previous_item        = list->last_item;
00182         item->next_item            = NULL;
00183         list->last_item->next_item = item;
00184         list->last_item            = item;
00185     }
00186 
00187     // Increment item count
00188     (list->nr_of_items)++;
00189 
00190     return TRUE;
00191 }

list_item_t* list_remove_first_item ( list_t list  ) 
Parameters:
list Pointer to the linked list
Returns:
list_item_t* Pointer to the (old) first item; NULL will be returned if the list is empty.

Definition at line 193 of file list.c.

References list_t::first_item, list_t::last_item, list_is_empty(), list_t::nr_of_items, and NULL.

Referenced by list_remove_item().

00194 {
00195     list_item_t *item = list->first_item;
00196 
00197     // See if list is empty
00198     if(list_is_empty(list))
00199     {
00200         return NULL;
00201     }
00202 
00203     // See if there is only one item
00204     if(list->first_item == list->last_item)
00205     {
00206         list->first_item = NULL;
00207         list->last_item  = NULL;
00208     }
00209     else
00210     {
00211         // The next item become the first one in the list
00212         list->first_item          = item->next_item;
00213         item->next_item->previous_item = NULL;
00214     }
00215 
00216     // Clear links of removed item
00217     item->previous_item = NULL;
00218     item->next_item     = NULL;
00219 
00220     // Decrement item count
00221     (list->nr_of_items)--;
00222 
00223     return item;
00224 }

list_item_t* list_remove_last_item ( list_t list  ) 
Parameters:
list Pointer to the linked list
Returns:
list_item_t* Pointer to the (old) last item; NULL will be returned if the list is empty.

Definition at line 226 of file list.c.

References list_t::first_item, list_t::last_item, list_is_empty(), list_t::nr_of_items, and NULL.

Referenced by list_remove_item().

00227 {
00228     list_item_t *item = list->last_item;
00229 
00230     // See if list is empty
00231     if(list_is_empty(list))
00232     {
00233         return NULL;
00234     }
00235 
00236     // See if there is only one item
00237     if(list->first_item == list->last_item)
00238     {
00239         list->first_item = NULL;
00240         list->last_item  = NULL;
00241     }
00242     else
00243     {
00244         // The previous item become the last one in the list
00245         list->last_item                = item->previous_item;
00246         item->previous_item->next_item = NULL;        
00247     }
00248 
00249     // Clear links of removed item
00250     item->previous_item = NULL;
00251     item->next_item     = NULL;
00252 
00253     // Decrement item count
00254     (list->nr_of_items)--;
00255 
00256     return item;
00257 }

void list_remove_item ( list_t list,
list_item_t item 
)
Parameters:
list Pointer to the linked list
item Item to be removed from the list

Definition at line 259 of file list.c.

References list_t::first_item, list_t::last_item, list_is_empty(), list_remove_first_item(), list_remove_last_item(), list_t::nr_of_items, and NULL.

00261 {
00262     // Extra sanity check
00263     if(list_is_empty(list))
00264     {
00265         return;
00266     }
00267 
00268     // See if this is the first item in the list
00269     if(item == list->first_item)
00270     {
00271         list_remove_first_item(list);
00272         return;
00273     }
00274 
00275     // See if this is the last item in the list
00276     if(item == list->last_item)
00277     {
00278         list_remove_last_item(list);
00279         return;
00280     }
00281 
00282     // Link previous and next item to each other
00283     item->previous_item->next_item = item->next_item;
00284     item->next_item->previous_item = item->previous_item;
00285     
00286     // Clear links of item
00287     item->previous_item = NULL;
00288     item->next_item     = NULL;
00289 
00290     // Decrement item count
00291     (list->nr_of_items)--;
00292 }

bool_t list_item_in_list ( list_t list,
list_item_t item 
)
Parameters:
list Pointer to the linked list
item Pointer to specified item
Return values:
TRUE Item is in the list
FALSE Item is not in the list

Definition at line 294 of file list.c.

References list_t::first_item, and NULL.

00296 {
00297     // Start at first item in the list
00298     list_item_t *item_in_list = list->first_item;
00299 
00300     // Search all items in the list
00301     while(item_in_list != NULL)
00302     {
00303         if(item_in_list == item)
00304         {
00305             // Item is in the list
00306             return TRUE;
00307         }
00308         // Next item
00309         item_in_list = item_in_list->next_item;
00310     }
00311     // Item is not in the list
00312     return FALSE;
00313 }

Generated on Fri Aug 13 16:50:38 2010 for Piconomic Firmware Library by doxygen 1.6.3