Linked list, printing looks destructive.
Salih Dincer
salihdb at hotmail.com
Mon Apr 25 10:48:24 UTC 2022
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;
}
struct List
{
Node * root, walker;
this(int[] AR)
{
foreach(i; AR)
{
pushfront(i);
}
}
bool empty() const
{
return !walker;
}
void popFront()
{
walker = walker.next;
}
float front() const
{
return walker.element;
}
void pushfront(int element)
{
Node * newnode = new Node();
newnode.element = element;
newnode.next = root;
root = newnode;
}
// shallow copy
this(ref return scope List that)
{
this.walker = that.root;
}
}//List
import std.stdio;
void main()
{
List list = List([3,2,1]);
//Node backupnode=l.node;
foreach(l; list)
l.writeln();
//l.node=backupnode;//Restore state destroyed by writeln
foreach(l; list)
l.writeln();
}
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list