Doubly-linked list/Traversal: Difference between revisions

(→‎{{header|Axe}}: Use different values on the links)
Line 1,072:
5
100
</pre>
 
=={{header|Oforth}}==
 
Complete definition is here : [[../Definition#Oforth]]
 
Defining #forEachNext and #forEachPrev allow to traverse this double linked list using #forEach: and #revEach: syntax
 
<lang oforth>DList method: forEachNext
{
dup ifNull: [ drop @head ifNull: [ false ] else: [ @head @head true] return ]
next dup ifNull: [ drop false ] else: [ dup true ]
}
 
DList method: forEachPrev
{
dup ifNull: [ drop @tail ifNull: [ false ] else: [ @tail @tail true] return ]
prev dup ifNull: [ drop false ] else: [ dup true ]
}
 
: test
{
| dl n |
DList new ->dl
dl insertFront("A")
dl insertBack("B")
dl head insertAfter(DNode new("C", null , null))
 
"Traversal (beginning to end) : " println
dl forEach: n [ n . ]
 
"\nTraversal (end to beginning) : " println
dl revEach: n [ n . ]
 
}</lang>
 
{{out}}
<pre>
>test
Traversal (beginning to end) :
A C B
Traversal (end to beginning) :
B C A ok
</pre>
 
1,015

edits