Looking for a Simple Doubly Linked List Implementation
snow jhon
sportsdz25 at gmail.com
Sat Sep 28 16:21:10 UTC 2019
On Saturday, 21 September 2019 at 18:52:23 UTC, Dennis wrote:
> 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.
Below is a simple doubly linked list with Garbage Collected
memory.
It's not performant or complete by any means, just a minimal
example in D like you wanted.
You probably also want methods for removing nodes or inserting in
the middle (else why don't you use an array?), I think you can
think of an implementation for those yourself (or look them up,
there should be plenty examples online).
More information about the Digitalmars-d-learn
mailing list