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

Josh foo at bar.com
Sun May 19 20:55:28 UTC 2019


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).

So off to the std.range documentation, which recommended reading 
http://ddili.org/ders/d.en/ranges.html, okay read that, boy if I 
want to display the contents of range I'm ALL set, to bad I would 
like to modify a list, so I seem to be no closer to my goal....oh 
and by the way if you think:

import std.stdio;
import std.range;

void main() {
     int[] slice = [ 1, 2, 3 ];
     int[] slice2 = slice;

     put(slice2, 100);

     writeln(slice2);
     writeln(slice);
}

Resulting in:

[2, 3]
[100, 2, 3]    ← expected result

Is obvious, I have some bad news for you.

Now I'm thinking maybe std.algorithm.comparison, maybe iteration 
or maybe mutation seems promising, at least that's what I want to 
do, mutate a list....hey there is a swap...oh it doesn't deal 
with ranges...

Maybe swapAt but that sounds like I need an index, and indexes 
with linked list don't really perform well...but it does take a 
range....okay I think that is what I need, so in a foreach loop, 
looking at https://tour.dlang.org/tour/en/basics/ranges I would 
call swapAt and pass in __rangeCopy, I think I can update, happy 
days, now to figure out how to add something before or after that 
element......


So in short I guess my complaint is that you show all this stuff 
about displaying ranges and removing items from the front or 
end...so if I want a queue we're set, but nothing about modifying 
them...unless it's so blatantly obvious I'm just missing it, 
which could be the case.



More information about the Digitalmars-d-learn mailing list