An arraylist (dynamic array) written in C.
typedef struct{
size_t data_size;
size_t max_length;
size_t length;
void *array;
int (* add)();
int (* remove)();
void* (* get)();
int (* set)();
int (* free)();
} ArrayList
Any of the methods that return an integer will return one of the corresponding values:
ALSUCCESS
: the method was successful
RMNODATA
: a remove was called, but there was no data in the list
ALIDXOOR
: Index Out Of Range
The get
method will return a null pointer if the index is out of bounds.
Both of these functions require the size of data type to be stored and will return a pointer to the struct ArrayList that is malloc'd.
ArrayList *newArrayList( size_t data_size );
Returns a pointer to the malloc'd ArrayList struct using a default max size of 10 items.
ArrayList *newArrayListSized( size_t data_size, size_t initial_length );
If the size of an array is known, use the initial length argument to set the initial max length of the array. Returns a pointer to the malloc'd ArrayList struct.
Assume that for each method there is already an allocated struct:
ArrayList *list = newArrayList( sizeof( int ) );
// Example
int data = 10;
list->add( list, &data );
Adds (copies) the data pointed to by data
to the next open space in the array.
// Example
list->remove( list, 10 );
Removes an item from the array at index index
.
// Example
int *data;
data = list->get( list, 2 );
printf( "%d", *data );
>>> 123
Returns a void pointer to the item in array index index
.
// Example
int data = 42;
list->set( list, 13, &data );
Copies the value at from data
to index
.
// Example
list->free( list );
Destroys all memory allocated for the ArrayList list
.
- ArrayLists are indexed starting at 0.
- An index can be a negative number. For example, choosing an index of -1 will return the last item in the array.