www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Iteratable single linked list of floats.

reply Alain De Vos <devosalain ymail.com> writes:
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
parent reply Alain De Vos <devosalain ymail.com> writes:
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
parent reply drug <drug2004 bk.ru> writes:
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
parent Alain De Vos <devosalain ymail.com> writes:
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