Linked list, printing looks destructive.

Alain De Vos devosalain at ymail.com
Mon Apr 25 15:56:05 UTC 2022


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();
		newnode.data=data;
		newnode.next=node;
		node=newnode;
	}//pushfront
	int opApply(int delegate(typeof(Node.data)) dg) {
		Node current = node;
		while (current) {
			if (dg(current.data)) return 1;
			current = current.next;
		}
		return 0;
	}//opApply
}//List

void main(){
	List l=new List([3,2,1]);
	foreach(element; l) writeln(element);
	foreach(element; l) writeln(element);
}//main
```


More information about the Digitalmars-d-learn mailing list