Re: Iteratable single linked list of floats.

2021-04-23 Thread Alain De Vos via Digitalmars-d-learn

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();

```



Re: Iteratable single linked list of floats.

2021-04-21 Thread drug via Digitalmars-d-learn

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


Re: Iteratable single linked list of floats.

2021-04-21 Thread Alain De Vos via Digitalmars-d-learn

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);
}
}
```


Iteratable single linked list of floats.

2021-04-21 Thread Alain De Vos via Digitalmars-d-learn
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