View mode: basic / threaded / horizontal-split · Log in · Help
June 03, 2003
How to implement a linked list in D?
I'm totally new to D so this may be a bit way over my head, but I know 
OO quite well and do think that I'm not too bad in C too. Now my question:

How do I implement a class Linked List?
I want it to have an item field and a next field which should probably be
Linked List again. 

I wrote this stuff which is so bad, that it does not even compile.

Could anyone give me a hand on how to achieve in D what I tries to explain?


what I want is something along this lines (Pseudo D)
class LinkedList
template Item (T){
T item;
LinkedList next;
... 



Is there a way to achieve that?

Or do I have to implement it in a more C-ish way (e.g with a struct?

Thanks for your time 

Friedrich
June 03, 2003
Re: How to implement a linked list in D?
try it like this:


template LinkedListTemplate(T)
{
   class LinkedList : T
   {
       LinkedList next;
   }
}

alias instance LinkedListTemplate(Foo).LinkedList FooLinkedList;


Unfortunately that way, it's not acceptable for Foo to be an alias for, say,
int or some other basic type.

You can still do it via inclusion instead of inheritance:

template LinkedListTemplate(T)
{
   class LinkedList
   {
       T item;
       LinkedList next;
   }
}

alias instance LinkedListTemplate(Foo).LinkedList FooLinkedList;

Sean

"Friedrich Dominivud" <Friedrich_member@pathlink.com> wrote in message
news:bbif3a$14s5$1@digitaldaemon.com...
> I'm totally new to D so this may be a bit way over my head, but I know
> OO quite well and do think that I'm not too bad in C too. Now my question:
>
> How do I implement a class Linked List?
> I want it to have an item field and a next field which should probably be
> Linked List again.
>
> I wrote this stuff which is so bad, that it does not even compile.
>
> Could anyone give me a hand on how to achieve in D what I tries to
explain?
>
>
> what I want is something along this lines (Pseudo D)
> class LinkedList
> template Item (T){
> T item;
> LinkedList next;
> ...
>
>
>
> Is there a way to achieve that?
>
> Or do I have to implement it in a more C-ish way (e.g with a struct?
>
> Thanks for your time
>
> Friedrich
June 03, 2003
Re: How to implement a linked list in D?
Shouldn't we have a linked list template inside phobos?
I think phobos should get important standard containers like list, map 
(other types than char[]), stack etc.
What do you think about it?

Cu Stephan
June 04, 2003
Re: How to implement a linked list in D?
Thanks, I'll try it the latter way. Which seems to be the "right" way.

Regards
Friedrich
June 04, 2003
Re: How to implement a linked list in D?
In article <bbinin$1d53$1@digitaldaemon.com>, Stephan Wienczny says...
>
>
>Shouldn't we have a linked list template inside phobos?
>I think phobos should get important standard containers like list, map 
>(other types than char[]), stack etc.
>What do you think about it?
>
>Cu Stephan
>
Well as posted before I'm new to D, but have a good working knowledge in Eiffel
and developed quite a few libraries. I can assure you that writing good
libraries is hard work and time consuming. But yes I think having a good
standard library is  important. 


Regards
Friedrich
June 09, 2003
Re: How to implement a linked list in D?
Hi, Friedrich.

Linked lists are the simplest of all data structures that most modern OO 
languages do poorly at.  In a Sather-like language, it is much simpler 
(please forgive the non-Sather syntax):

module LinkedList;

class Parent {
    Child *head;
}

class Child {
    Child *next;
}

To reuse this code in Sather takes an "include" statement like:

include LinkedList {
    Parent -> MyDocumentManager;
    Child -> MyDocument;
}

The guy who wrote the C++ STL said he uses the "max" function as a 
simple test of code reusability in a language.  That's a good one.  I 
also like the linked list.  Even in Eiffel, the implementation of linked 
lists is terrible.

Bill

Friedrich Dominivud wrote:
> I'm totally new to D so this may be a bit way over my head, but I know 
> OO quite well and do think that I'm not too bad in C too. Now my question:
> 
> How do I implement a class Linked List?
> I want it to have an item field and a next field which should probably be
> Linked List again. 
> 
> I wrote this stuff which is so bad, that it does not even compile.
> 
> Could anyone give me a hand on how to achieve in D what I tries to explain?
> 
> 
> what I want is something along this lines (Pseudo D)
> class LinkedList
> template Item (T){
> T item;
> LinkedList next;
> ... 
> 
> 
> 
> Is there a way to achieve that?
> 
> Or do I have to implement it in a more C-ish way (e.g with a struct?
> 
> Thanks for your time 
> 
> Friedrich
> 
> 
>
Top | Discussion index | About this forum | D home