I want to create a linked list with pointers. So I created a struct with a pointer and a value, the pointer being the one pointing to the next node of the list.
When I tried to set a poniter to the next node, i wanted to change the value of the next node by pointing at it from the first node, but when I try to show the value by accesing directly from the node structure i get another result, how can I make those to be equal? So that if I change one, no matter from where, the node changes its value.
This is for a university project where we need to use linked lists. I am just starting on the pointers subject so I accept any help.
#include <iostream>
struct node
{
double val;
node *nxt;
};
int main()
{
node n1, n2;
n1.val = 10;
n1.nxt = new node{n2};
n1.nxt->val = 20;
std::cout << "List:\n";
std::cout << n1.val << "\n"
<< n2.val << "\n";
}
Actual result:
List:
10
1.39065e-309
Expected result:
List:
10
20