-
Notifications
You must be signed in to change notification settings - Fork 1
/
sll.fs
50 lines (36 loc) · 1.02 KB
/
sll.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
\ *******************************
\ Singularly Linked Lists
\ *******************************
{
\ private structure parts
0
cell field >next \ pointer to next node
: empty ( head -- h 1 ) \ helper leaves word if empty list
dup @ dup 0= if 2drop pull drop exit then ;
public
: l_add ( node head -- ) \ add node to list head
2dup @ swap ! !
;
: l_rm ( head -- ) \ remove node at head
empty @ swap ! ;
: l_top ( head -- ) \ returns node at top of list
@ ;
\ Remove and return next node from list
: l_dequeue ( handle -- node )
dup @ swap l_rm ;
: l_head ( "name" -- ) \ create a list head
0 variable
;
\ The passed xt's is prototyped like follows: ( a -- f )
: l_dountil ( xt h -- n | 0 ) \ interate through a list
begin @ dup
while 2dup push push swap exec
if pull drop pull exit then
pull pull
repeat nip
;
\ count number of nodes in list
: l_count ( h -- u )
0 swap {{ drop 1+ false }} swap l_dountil drop ;
struct list
}