Doubly-linked list/Element definition: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Wren}}: Rewrote this as the task is about element definition rather than the definition of the doubly-linked list itself.)
Line 1,005: Line 1,005:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-llist}}
{{libheader|Wren-llist}}
The DLinkedList class in the above module is a generic doubly-linked list and is implemented in such a way that circular loops are not possible. We therefore use it here.
The DNode class in the above module is the element type for the DLinkedList class which is a generic doubly-linked list. The latter is implemented in such a way that the user does not need to deal directly with DNode though for the purposes of the task we show below how instances of it can be created and manipulated.
<lang ecmascript>import "/llist" for DLinkedList
<lang ecmascript>import "/llist" for DNode


var dll = DLinkedList.new()
var dn1 = DNode.new(1)
for (i in 1..3) dll.add(i)
var dn2 = DNode.new(2)
dn1.next = dn2
System.print(dll)
dn1.prev = null
for (i in 1..3) dll.remove(i)
dn2.prev = dn1
System.print(dll)</lang>
dn2.next = null
System.print(["node 1", "data = %(dn1.data)", "prev = %(dn1.prev)", "next = %(dn1.next)"])
System.print(["node 2", "data = %(dn2.data)", "prev = %(dn2.prev)", "next = %(dn2.next)"])</lang>


{{out}}
{{out}}
<pre>
<pre>
[node 1, data = 1, prev = null, next = 2]
[1 <-> 2 <-> 3]
[node 2, data = 2, prev = 1, next = null]
[]
</pre>
</pre>