Looking for a Simple Doubly Linked List Implementation

Dennis dkorpel at gmail.com
Sat Sep 21 18:52:23 UTC 2019


On Saturday, 21 September 2019 at 08:34:09 UTC, Ron Tarrant wrote:
> Thanks, Dennis. Not performant... It doesn't work? I was hoping 
> for a complete, working example, but maybe this'll help.

Bad word choice (it appears it's debatable whether 'performant' 
even is a word), I meant it was a simple implementation not 
optimized for speed / memory efficiency.
Making it 'complete' is a bit hard since I can think of tens of 
methods and operator overloads you could use, but if I include 
them all it's no longer minimal and it just becomes 
std.container.dlist.

> Does a doubly-linked list always have to be done with structs? 
> Can it be classes instead?

My example originally included classes actually. It was mostly 
the same, except that Node!T* was just Node!T. The only problem 
was with const:

```
size_t length() const {
     size_t result = 0;
     for(auto a = head; a !is null; a = a.next) result++;
     return result;
}

```

Since I marked the method as const, `auto a = head` got the type 
const(Node!T) and `a = a.next` no longer compiled. With structs 
you can declare a const(Node!T)* (mutable pointer to const node), 
but I don't know if I can declare a mutable reference to a const 
class, so I switched to structs.


More information about the Digitalmars-d-learn mailing list