Looking for a Simple Doubly Linked List Implementation

Ron Tarrant rontarrant at gmail.com
Sat Sep 21 09:03:13 UTC 2019


On Saturday, 21 September 2019 at 08:49:48 UTC, ag0aep6g wrote:
> On 21.09.19 10:34, Ron Tarrant wrote:
>> Here's a question for the room:
>> 
>> Does a doubly-linked list always have to be done with structs? 
>> Can it be classes instead? (Maybe that's why I can't get it to 
>> work, because I've been trying to make an OOP version?)
>
> It can be done with classes.
>
>> When I run the following code, it gets through creating the 
>> list head and the first node, then seems to get stuck in an 
>> infinite loop. Here's the code:
> [...]
>> class Tab
>> {
> [...]
>>      Tab* _prev = null, _next = null;
> [...]
>>      Tab* getNext()
> [...]
>>      Tab* getPrev()
> [...]
>>      void setNext(Tab* tab)
> [...]
>>      void setPrev(Tab* tab)
> [...]
>> } // class Tab
>
> Your mistake is that you're using pointers. `Tab` is a class. 
> That means values of the type are already references. There is 
> no need for `Tab*`. Just use `Tab` wherever you have `Tab*` 
> now, and get rid of any addr-ofs (`&foo`) and dereferendces 
> (`*bar`) you have.

Ah! Thanks, ag0aep6g. I was wondering about that when I was 
writing the code. (If I already knew this, I'd forgotten.) I did 
as you suggested, took out all '*' and '&' and it works perfectly.


More information about the Digitalmars-d-learn mailing list