Singly-linked list/Element insertion: Difference between revisions
Content added Content deleted
(→{{header|Groovy}}: new solution) |
(Updated D entry) |
||
Line 266: | Line 266: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>struct SLinkedNode(T) { |
|||
This is similar to C++ example. |
|||
T data; |
|||
typeof(this)* next; |
|||
this(T data_, typeof(this)* next_=null) { |
|||
<lang D>Node!(T) insert_element_after(T)(Node!(T) dst, Node!(T) src) { |
|||
this.data = data_; |
|||
this.next = next_; |
|||
} |
|||
} |
|||
}</lang> |
|||
void insertAfter(T)(SLinkedNode!T* listNode, SLinkedNode!T* newNode) { |
|||
Sample usage (tango based): |
|||
newNode.next = listNode.next; |
|||
⚫ | |||
listNode.next = newNode; |
|||
{ |
|||
} |
|||
⚫ | |||
⚫ | |||
auto c = new NodeStr("charlie"); |
|||
⚫ | |||
insert_element_after(a, c); |
|||
⚫ | |||
⚫ | |||
// print out |
|||
auto c = new N('C'); |
|||
Stdout(a.data) (" -> "); |
|||
// Inserts C after A, creating the (A C B) list: |
|||
a = a.next; |
|||
insertAfter(lh, c); |
|||
} |
|||
// |
// The GC will collect the memory. |
||
}</lang> |
}</lang> |
||