digitalmars.D.learn - Iteratable single linked list of floats.
- Alain De Vos (27/27) Apr 21 2021 I try to create manually and explicit an interetable single
- Alain De Vos (26/26) Apr 21 2021 Formatted ,
- drug (33/56) Apr 21 2021 ```D
- Alain De Vos (46/46) Apr 23 2021 Here a working code,
I try to create manually and explicit an interetable single linked list of floats. Probably one of the most basic datastructures. import std.stdio; void main(){ struct List { struct Node { float f; Node *next; } Node * root=null; bool empty() const {return !root;} void popFront() {root=root.next;} float front() const {return root.f;} void push(float f) { Node * newnode=new Node(); newnode.f=f; root.next=newnode; // Segmentation fault } } List * l=new List(); l.push(3); foreach(element;l){ // Invalid foreach aggregate writeln(element.root.f); } } But I have a segmentation fault and an invalid foreach aggregate
Apr 21 2021
Formatted , ``` import std.stdio; void main(){ struct List { struct Node { float f; Node *next; } Node * root=null; bool empty() const {return !root;} void popFront() {root=root.next;} float front() const {return root.f;} void push(float f) { Node * newnode=new Node(); newnode.f=f; root.next=newnode; // Segmentation fault } } List * l=new List(); l.push(3); foreach(element;l){ // Invalid foreach aggregate writeln(element.root.f); } } ```
Apr 21 2021
21.04.2021 16:19, Alain De Vos пишет:import std.stdio; void main(){ struct List { struct Node { float f; Node *next; } Node * root=null; bool empty() const {return !root;} void popFront() {root=root.next;} float front() const {return root.f;} void push(float f) { Node * newnode=new Node(); newnode.f=f; root.next=newnode; // Segmentation fault } } List * l=new List(); l.push(3); foreach(element;l){ // Invalid foreach aggregate writeln(element.root.f); } }```D import std.stdio; void main(){ struct List { struct Node { float f; Node *next; } Node * root=null; bool empty() const {return !root;} void popFront() {root=root.next;} float front() const {return root.f;} void push(float f) { Node * newnode=new Node(); newnode.f=f; if (root) // by default root is null so you need to initialize it first time root.next=newnode; else root = newnode; } } List * l=new List(); l.push(3); foreach(element; *l){ // Invalid foreach aggregate because `l` is a pointer to List, so you need to dereference the pointer writeln(element); } } ``` 1) you need to initialize the root 2) pointer to range is not valid foreach aggregate
Apr 21 2021
Here a working code, ``` import std.stdio; void main(){ struct List { struct Node { float f; Node *next=null; } Node * root=null; bool empty() const {return !root;} void popFront() {root=root.next;} float front() const {return root.f;} void pushfront(float f) { Node * newnode=new Node(); newnode.f=f; newnode.next=root; root=newnode; } void pushend(float f){ Node * newnode=new Node(); newnode.f=f; Node *t=root; if(t==null) {t=newnode;} else{ while(t!=null && t.next!=null) {t=t.next;} t.next=newnode; } } void printall(){ Node *l=root; for( ; l ; l=l.next){ writeln(l.f); } } } List * l=new List(); l.pushfront(2); l.pushfront(1); l.pushend(3); l.pushend(4); foreach(element; *l) writeln(element); (*l).printall(); ```
Apr 23 2021