D - How to implement a linked list in D?
- Friedrich Dominivud (17/17) Jun 03 2003 I'm totally new to D so this may be a bit way over my head, but I know
- Sean L. Palmer (25/42) Jun 03 2003 try it like this:
- Stephan Wienczny (5/5) Jun 03 2003 Shouldn't we have a linked list template inside phobos?
- Friedrich (7/12) Jun 03 2003 Well as posted before I'm new to D, but have a good working knowledge in...
- Friedrich Dominicus (3/3) Jun 03 2003 Thanks, I'll try it the latter way. Which seems to be the "right" way.
- Bill Cox (22/53) Jun 09 2003 Hi, Friedrich.
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
Jun 03 2003
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 toexplain?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
Jun 03 2003
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
Jun 03 2003
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 StephanWell 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
Jun 03 2003
Thanks, I'll try it the latter way. Which seems to be the "right" way. Regards Friedrich
Jun 03 2003
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
Jun 09 2003