0

I always get null value. I am beginner in this language. Thank you for understanding.

class Node{
    int data;
    Node next;

    Node(int value){
        data = value;
    } 
}

void addNode(Node headNode, int aValue){
    Node newNode = new Node(aValue); //creating new node
    newNode.next = headNode;         // pointing the next to headNode
    headNode = newNode;              // updating the head Node
}

void main(){
    Node head = Node(1);
    addNode(head,2);
    addNode(head,3);

    print(head.next.data); //here I always get null
}

Your help will truely be appreciated.

Eric Echemane
  • 94
  • 1
  • 9

1 Answers1

1

Consider where you update the value of headNode. What exactly are you assigning to there?

headNode has function scope, which means it only exists within the addNote function. Similarly head in your main function also has function scope, so it is only accessible in your main function.

It seems like you are trying to update head from within addNode but that is not what you have here.

One possible solution would be to return the newly created node from addNode and use the returned value to reassign head in the main function. It would look something like this:

Node addNode(Node headNode, int aValue){
    Node newNode = new Node(aValue);
    newNode.next = headNode;
    return newNode;
}

void main(){
    Node head = Node(1);
    head = addNode(head,2);
    head = addNode(head,3);

    print(head.next.data);
}

One more thing to think about: What does your list look like when execution reaches the end of your program? Is it [1] > [2] > [3] or [3] > [2] > [1] ?

Stephen
  • 4,041
  • 22
  • 39
  • Respectively, we can see that the structure is like a stack. I created the addNode to automate the process of linking the nodes together. In stack, the very last inserted node will be the head or the first that comes out. And that is my question, is it possible in dart to pass the hedNode as a reference so that I can manipulate it in the memory? – Eric Echemane Oct 29 '20 at 03:05
  • meaning list will look like this expectedly...3 > 2 > 1 – Eric Echemane Oct 29 '20 at 03:07
  • 1
    Sorry, your question is unclear to me. `headNode` is passed by reference into `addNode`. I am not sure what you are getting at when you say you want to manipulate it in memory. In regards to insertion order, the solution in my answer will indeed push nodes onto the top of the stack. If that is what you are intending, then the code should be good to go as is. When you want to pop the stack simply take the `data` value at `head` and then replace `head` with `head.next` – Stephen Oct 29 '20 at 04:59
  • 1
    @EricEchemane No, everything in Dart is pass-by-value (or arguably, [pass-by-assignment](https://stackoverflow.com/questions/25170094/) might be a clearer term). If you want a function to modify a value, [you'll need to wrap it in another object first](https://stackoverflow.com/q/55276882/179715). In this case, you'd be best off creating a separate `LinkedList` class that stores references to the head and tail. Or just use the existing [`LinkedList` class](https://api.dart.dev/stable/dart-collection/LinkedList-class.html) from `dart:collection`. – jamesdlin Oct 29 '20 at 05:25
  • 1
    @jamesdlin You are right, everything in Dart is pass by value. I was simplifying things a bit for my answer. I thought that trying to explain how everything is pass by value, yet objects can be mutated from within functions while primitives cannot was more than would fit nicely in a comment. Your link is a good one though. While Eric could just use LinkedList from dart:collection, if he is just learning, there is value in implementing it himself to understand how it works. – Stephen Oct 29 '20 at 07:52