Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
Indeed code below works, ``` import std.stdio: write,writeln; class Node { int data; Node next; } class List { Node node=null; this(int[] AR){foreach(i ; AR)pushfront(i);} void pushfront(int data) { Node newnode=new Node();

Re: Linked list, printing looks destructive.

2022-04-25 Thread cc via Digitalmars-d-learn
On Monday, 25 April 2022 at 01:40:01 UTC, Alain De Vod wrote: Following program is a single linked list. We expect as output 1 2 3 1 2 3 But the output is only 1 2 3 ``` If you don't need List to be treated as a true range, but just want to iterate, a simple way to do this is with opApply:

Re: Linked list, printing looks destructive.

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 4/25/22 03:48, Salih Dincer wrote: > It is also possible with the copy constructor of a struct. I don't know > how to do with class... Classes don't have language provided construction because nobody needs it and in fact they have to protect themselves when a language provides it. (See,

Re: Linked list, printing looks destructive.

2022-04-25 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 April 2022 at 09:38:05 UTC, Alain De Vos wrote: This program works ok, (but List is no Range) It is also possible with the copy constructor of a struct. I don't know how to do with class... ```d struct Node { int element; Node * next; }

Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
This program works ok, (but List is no Range) ``` class Node { int data; Node next; } class List { Node node=null; this(int[] AR){foreach(i ; AR)pushfront(i);} bool empty() const {return !node;} void popFront() {node=node.next;} float

Re: Linked list, printing looks destructive.

2022-04-25 Thread Alain De Vos via Digitalmars-d-learn
On Monday, 25 April 2022 at 05:17:28 UTC, Salih Dincer wrote: On Monday, 25 April 2022 at 02:19:46 UTC, Ali Çehreli wrote: This type violates a fundamental rule: Containers and ranges are separate concepts. Your List is a container, not a range. I changed your code by moving the range

Re: Linked list, printing looks destructive.

2022-04-24 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 25 April 2022 at 02:19:46 UTC, Ali Çehreli wrote: This type violates a fundamental rule: Containers and ranges are separate concepts. Your List is a container, not a range. I changed your code by moving the range functions to a Range [...] Dear Ali, I implemented a linkedlist

Re: Linked list, printing looks destructive.

2022-04-24 Thread Ali Çehreli via Digitalmars-d-learn
On 4/24/22 18:40, Alain De Vod wrote: > I think this has something to do with popFront This type violates a fundamental rule: Containers and ranges are separate concepts. Your List is a container, not a range. I changed your code by moving the range functions to a Range struct that is created

Linked list, printing looks destructive.

2022-04-24 Thread Alain De Vod via Digitalmars-d-learn
Following program is a single linked list. We expect as output 1 2 3 1 2 3 But the output is only 1 2 3 I think this has something to do with popFront How to fix it using "class List" ? ``` import std.stdio: write,writeln; import std.range: empty,popFront,front; struct Node { int