Linked List iterating over and inserting an element around (before/after) the current position.

Paul Backus snarwin at gmail.com
Sun May 19 21:10:35 UTC 2019


On Sunday, 19 May 2019 at 20:55:28 UTC, Josh wrote:
> Just started looking at D this weekend, coming from a 
> C++/Java/Go/Rust background and it's really not going well.  
> Trying to write something to play with the language and need a 
> linked list, looking in std.container you have a single or 
> doubly linked list...great.
>
> Now how to I iterate over it and look for a value and then 
> modify it, and maybe insert a new element before or after that 
> element.....After spending way to long on the API realized I 
> need to be looking at std.range (I think, maybe not, I'm not 
> sure).

Here's an example using std.container.dlist:

import std.stdio;
import std.container.dlist;
import std.algorithm;

void main()
{
     auto list = make!DList("the", "quick", "brown", "fox");
     auto range = list[].find("quick");
     range.front = "slow";
     list.insertBefore(range, "really");
     list[].each!writeln; // The really slow brown flox
}

Interactive version: https://run.dlang.io/is/j1Qa8y


More information about the Digitalmars-d-learn mailing list