| 1 |
SList: a C library that implements Singly Linked Lists for classic Macintosh programs. |
| 2 |
|
| 3 |
SList is free software; see the LICENSE file for copyright/licensing |
| 4 |
--- |
| 5 |
|
| 6 |
# Install |
| 7 |
|
| 8 |
This library has been built and tested using THINK C v5 on a Macintosh SE. |
| 9 |
|
| 10 |
In order to install it, just import `slist.c` and `slist.h` to your project and include `slist.h` in your source files. |
| 11 |
|
| 12 |
--- |
| 13 |
|
| 14 |
# Usage |
| 15 |
|
| 16 |
## Creating a new list |
| 17 |
``` |
| 18 |
// Create a new SList object. |
| 19 |
SList* my_list = SList_new(); |
| 20 |
``` |
| 21 |
|
| 22 |
## Adding objects to the list |
| 23 |
``` |
| 24 |
// Append an object at the end of the list. |
| 25 |
slist_append(my_list, (Ptr)my_object); |
| 26 |
|
| 27 |
// Insert an object at a given 0 based index position in the list. |
| 28 |
// (Third position in this case). |
| 29 |
slist_insert_at(my_list, (Ptr)my_object, 2); |
| 30 |
|
| 31 |
// Insert an object after the first occurence of another one. |
| 32 |
slist_insert_after(my_list, (Ptr)my_object, (Ptr)other_object); |
| 33 |
``` |
| 34 |
|
| 35 |
## Removing objects from the list |
| 36 |
``` |
| 37 |
// Remove the first occurence of an object from the list. |
| 38 |
slist_remove_value(my_list, (Ptr)my_object); |
| 39 |
|
| 40 |
// Remove an object at a given 0 based index postion from the list. |
| 41 |
// (Third position in this case). |
| 42 |
slist_remove_at(my_list, 2); |
| 43 |
|
| 44 |
// Remove the last object from the list. |
| 45 |
slist_remove_last(my_list); |
| 46 |
|
| 47 |
// Remove all objects from the list. |
| 48 |
slist_empty(my_list); |
| 49 |
``` |
| 50 |
|
| 51 |
## Parsing a list |
| 52 |
``` |
| 53 |
SL_FOREACH(item, list) { |
| 54 |
obj = (MyObject*)item->data; |
| 55 |
} |
| 56 |
``` |
| 57 |
|
| 58 |
You can customize the SL_FOREACH macro by re-implementing `SList.first()`, `SList.last()` and `SList.next()` for your own needs. |