Linked list, printing looks destructive.
Alain De Vod
devosalain at ymail.com
Mon Apr 25 01:40:01 UTC 2022
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 element;
Node * next;
}
class List {
Node * root=null;
this(int[] AR){foreach(i ; AR)pushfront(i);}
bool empty() const {return !root;}
void popFront() {root=root.next;}
float front() const {return root.element;}
void pushfront(int element) {
Node * newnode=new Node();
newnode.element=element;
newnode.next=root;
root=newnode;
}
}//List
void main(){
List l=new List([3,2,1]);
foreach(element; l) writeln(element);
foreach(element; l) writeln(element);
}
```
More information about the Digitalmars-d-learn
mailing list