Reputation: 111
I've been attempting a function where you add a node to the end of a linked list, and while appearing successful, it appears to only fill the head value and not continue the chain. This function affects the list, goes back to the main function, then accesses the same linked list for other functions, yet I can only access the head of the linked list.
I've looked at multiple past questions and examples for as to why this is occurring, but none of them have quite the same issue and situation as me.
This is in the addNode function, with node *&head as input from the main function. And for the purposes of this, I have filled A's values with input already, that is functioning properly.
node *A = new node, *ptr = head;
A->costs1=costs;
A->next = nullptr;
ptr = head;
if (head == nullptr)
{
head = A;
}
else{
while (ptr->next!=nullptr){
ptr=ptr->next;
}
ptr = A;
}
And here is the struct, I pared down the amount of variables so it wouldnt get too complex:
struct node
{
string costs1;
node *next;
};
I am able to access head->costs1 just fine, and it is correct, but if I try and access head->next->costs1 it ends up with nothing.
Upvotes: 0
Views: 209
Reputation: 33931
lxop's answer should solve the problem, but there is a better option. I tried to explain it in the comments, but comments suck at explaining anything complicated.
So here we go:
//set up the new node.
// Nothing new here.
node *A = new node;
A->costs1=costs;
A->next = nullptr;
// Find the null next pointer (the end of the list)
// head is a pointer to the next node, just like any other next pointer.
// this hides the difference between head and next so we can use the exact
// same code for both. This reduces the number of cases we need to handle.
// In addition by having a pointer to the next pointer, we are tracking
// node insertion points, not nodes. When you are tracking the nodes, you're
// forced to keep track of the current node and the previous node so you
// can get access to the previous node's next so you can link the new new
// node to it. With the pointer to pointer, you have a pointer to previous's
// next, you can assign right to it AND use it as the current node.
node **ptr = &head; // get pointer to pointer to next node, the first node
// in this case
while (*ptr != nullptr){ // test that there is a next node, if there isn't
// we've found the end.
ptr = &(*ptr)->next; // if there is, advance to point at it's next.
}
// link in the new node
// when there are no more nodes to be had, ptr points right at the next node
// we need to update
*ptr = A; // so we update it.
and here it is again without all the explanation for clarity:
//set up the new node
node *A = new node;
A->costs1=costs;
A->next = nullptr;
// Find the null next pointer (the end of the list)
node **ptr = &head;
while (*ptr != nullptr){
ptr = &(*ptr)->next;
}
// link in the new node
*ptr = A;
Upvotes: 0
Reputation: 8595
You need to set ptr->next = A
, rather than ptr = A
.
Your current code doesn't add the A object into the list, it just sets the ptr
pointer to it, which isn't used.
Upvotes: 1