Traverse from the beginning of a singly-linked list to the end.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


with javascript_semantics
enum NEXT,DATA
constant empty_sll = {{1}}
sequence sll = deep_copy(empty_sll)
 
procedure insert_after(object data, integer pos=length(sll))
    sll = append(sll,{sll[pos][NEXT],data})
    sll[pos][NEXT] = length(sll)
end procedure
 
insert_after("ONE")
insert_after("TWO")
insert_after("THREE")
 
?sll
 
procedure show()
integer idx = sll[1][NEXT]
    while idx!=1 do
        ?sll[idx][DATA]
        idx = sll[idx][NEXT]
    end while
end procedure
show()
