My ordered_insert function crashes when I try to run it, I need help with the pointers but I'm pretty sure the logic is right. It compiles, but crashes when I run the exe.
You're trying to dereference a null pointer on line 33:
1 2 3 4
//If empty list
if ( *cur == NULL && *prev == NULL )
return NULL;
}
You declared **prev = NULL at the beginning of sll_node, and you never change it, since you are passing *front (which is NULL) to the function, so the value of **cur (A pointer to *front) is NULL and your for loop doesn't run.
I would get rid of the double pointers completely. I have no idea what you're trying to achieve with them.
It doesn't work still, it still crashes when I execute it. I think I need to use pointers to pointers since if the node to insert is smallest, I need to modify the original front pointer to point to the new node...So I'm sticking w/ my original code, but not sure what to do now...I hope someone can help out!
Here is new code and function againI forgot to add to insert new node at end if it's node w/ "largest" value:
If you used a double pointer to update the frontnode that you store in your code, how are you going to back track through your data to the stuff stored before frontnode? You don't have any pointers pointing to previous nodes (Since this would be a doubly linked list). I didn't look over all your code, because to be perfectly honest it's quite chaotic, but I think something like this would do the trick:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Insert(Node *first, int new_data)
{
while(first->next != NULL)
{
if(first->next->data > new_data)
{
Node *temp = new Node(new_data);
temp->next = first->next;
first->next = temp;
return;
}
first = first->next;
}
first->next = new Node(new_data);
}
There is one problem with my code though, and that's if the first node is greater then it won't be replaced. See if you can fix that. I haven't actually been coding for that long, so thanks for saying that. Although I still consider myself a beginner.