Bsoft 2.1.4
Bernard's software package
linked_list.h File Reference

Header file for generalized linked list functions. More...

#include <stdlib.h>
#include <string.h>

Functions

char * add_item (char **list, unsigned long size)
 Adds an item to a linked list. More...
 
char * append_item (char **list, char *item)
 Appends an item to a linked list. More...
 
char * copy_item (char *toitem, char *fromitem, unsigned long size)
 Copies an item to an existing item without changing the link. More...
 
char * remove_item (char **list, char *item, unsigned long size)
 Finds the given item and deletes it from the linked list. More...
 
char * replace_item (char **list, char *item, char *new_item)
 Finds the given item and replaces it with the new item. More...
 
char * copy_list (char *list, unsigned long size)
 Generates a complete copy of a linked list. More...
 
long kill_list (char *list, unsigned long size)
 Frees all the items in a linked list. More...
 
long count_list (char *list)
 Counts the number of items in a linked list. More...
 
long reverse_list (char **list)
 Reverse the order of items in a linked list. More...
 

Detailed Description

Header file for generalized linked list functions.

Author
Bernard Heymann
Date
Created: 20031203
Modified: 20150206

Function Documentation

◆ add_item()

char * add_item ( char **  list,
unsigned long  size 
)

Adds an item to a linked list.

Parameters
**listpointer to first item in the list.
sizesize of item.
Returns
char* new item.
If the list is not defined, the new item becomes the first in the list.
Otherwise, the list is traversed to find the end and the new item appended.
Any structure with a pointer to itself as a first element can be used in a
linked list. However, a linked list can only consist of one type of structure.

◆ append_item()

char * append_item ( char **  list,
char *  item 
)

Appends an item to a linked list.

Parameters
**listpointer to first item in the list.
*itemitem to append.
Returns
char* item.
If the list is not defined, the new item becomes the first in the list.
Otherwise, the list is traversed to find the end and the new item appended.
Any structure with a pointer to itself as a first element can be used in a
linked list. However, a linked list can only consist of one type of structure.

◆ copy_item()

char * copy_item ( char *  toitem,
char *  fromitem,
unsigned long  size 
)

Copies an item to an existing item without changing the link.

Parameters
*toitemitem to copy to.
*fromitemitem to copy from.
sizesize of item.
Returns
char* the resultant item.
The items must be the same type.

◆ copy_list()

char * copy_list ( char *  list,
unsigned long  size 
)

Generates a complete copy of a linked list.

Parameters
*listlinked list.
sizesize of list item.
Returns
char* new list.

◆ count_list()

long count_list ( char *  list)

Counts the number of items in a linked list.

Parameters
*listfirst item in the linked list.
Returns
long number of items in the list.

◆ kill_list()

long kill_list ( char *  list,
unsigned long  size 
)

Frees all the items in a linked list.

Parameters
*listfirst item in the linked list.
sizesize of item.
Returns
long number of items deallocated.
The list is traversed, setting a pointer to the next item before
deallocating the current item.

◆ remove_item()

char * remove_item ( char **  list,
char *  item,
unsigned long  size 
)

Finds the given item and deletes it from the linked list.

Parameters
**listpointer to first item in the linked list.
*itemitem to be deleted.
sizesize of item.
Returns
char* item after the one removed.
If the item is the first in the list, the list pointer is set to point
to the next item.
Otherwise, the list is traversed to find the item, the previous item's
pointer is set to the next item, and the current item deallocated.

◆ replace_item()

char * replace_item ( char **  list,
char *  item,
char *  new_item 
)

Finds the given item and replaces it with the new item.

Parameters
**listpointer to first item in the linked list.
*itemitem to be replaced.
*new_itemnew item.
Returns
char* new item.
If the item is the first in the list, the list pointer is set to point
to the new item.
Otherwise, the list is traversed to find the item, the previous item's
pointer is set to the new item, the new item's pointer is set to that
of the old item, and the old item is deallocated.

◆ reverse_list()

long reverse_list ( char **  list)

Reverse the order of items in a linked list.

Parameters
*listfirst item in the linked list.
Returns
long number of items in the list.