A linked list is a linear collection of data elements, where the order is not given by their physical locations in memory. Each element (called a node) has a secondary field that points to the next.
+----+---+ +----+---+ | 11 | ----->| 22 | x | +----+---+ +----+---+
This implements a basic linked list in RETRO. The words here are in a ll namespace. I'm not focusing on short names here; I expect that anything making extensive use of this will provide a set of aliases or extensions to make things more readable. E.g.,
#1 ll:node-n #2 ll:node-n ll:append #3 ll:node-n ll:append #4 ll:node-n ll:append
It's easy to write wrappers that allow for things like:
#1 < #2 > #3 > #4 >
A node is a simple structure with two fields. The first (which I am calling head) holds a value and the second (next) holds a pointer to the next node in the list. If the next field points to an address of zero it is considered the end of the list.
ll:head (a-a) return the address of the head field for a node ll:next (a-a) return the address of the next field for a node ll:node-n (n-a) create a new node with a head of n and a next of 0 ll:node (-a) create a new node with both fields set to 0
~~~ :ll:head ; :ll:next n:inc ;
:ll:node-n here swap , #0 , ; :ll:node here #0 , #0 , ;