Singly-linked list/Reversal: Difference between revisions

Added Wren
(→‎{{header|ALGOL 68}}: And another type...)
(Added Wren)
Line 45:
Big fjords vex quick waltz nymph
nymph waltz quick vex fjords Big
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-llist}}
{{libheader|Wren-iterate}}
The LinkedList class in Wren-llist represents a singly-linked list.
 
It's possible to iterate backwards through this without creating an intermediate list by traversing through it until you reach the last element, then traversing through it again until you reach the penultimate element and so on until you reach the first element. This is easy to code since LinkedList has an indexer which works in precisely this manner.
 
It's also possible to iterate backwards through the linked list using the generic reverse iterator in Wren-iterate. However, this does create a list internally and then iterates backwards through that.
<syntaxhighlight lang="ecmascript">import "./llist" for LinkedList
import "./iterate" for Reversed
 
var pangram = "Big fjords vex quick waltz nymph"
var elements = pangram.split(" ")
var sll = LinkedList.new(elements)
 
// iterate forwards
for (e in sll) System.write("%(e) ")
System.print("\n")
 
// iterate backwards without creating a list
for (i in sll.count-1..0) System.write("%(sll[i]) ")
System.print("\n")
 
// iterate backwards by creating a list internally
for (e in Reversed.new(sll)) System.write("%(e) ")
System.print()</syntaxhighlight>
 
{{out}}
<pre>
Big fjords vex quick waltz nymph
 
nymph waltz quick vex fjords Big
 
nymph waltz quick vex fjords Big
</pre>
9,486

edits