Jump to content

Singly-linked list/Traversal: Difference between revisions

Updated first D entry
No edit summary
(Updated first D entry)
Line 145:
 
=={{header|D}}==
<lang d>struct SLinkedNode(T) {
Traversal using list defined in [[Singly-Linked_List_(element)#D | Singly-Linked list element - D]].
T data;
<lang D>// a is a beginning of a list);
typeof(this)* next;
while (a) {
Stdout(a.data) (" -> ");
a = a.next;
}</lang>
 
this(T data_, typeof(this)* next_=null) {
Or using tango's collections (written by Doug Lea, ported to D)
this.data = data_;
this.next = next_;
}
}
 
void main() {
<lang D>import tango.io.Stdout;
import std.stdio;
 
alias SLinkedNode!int N;
auto lh = new N(1, new N(2, new N(3, new N(4))));
 
auto p = lh;
while (ap) {
Stdout write(ap.data), (" -> ");
a p = ap.next;
}
writeln();
}</lang>
{{out}}
<pre>1 2 3 4 </pre>
===Alternative Version===
Or usingUsing tango's collections (written by Doug Lea, ported to D):
<lang Dd>import tango.io.Stdout;
import tango.util.collection.LinkSeq;
 
Line 163 ⟶ 181:
m.append("charlie");
foreach (val; m)
Stdout (val).newline;
}</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.