I have this program that sorts an added list but something is wrong in the add function, I've been using breakpoints, something is wrong with the whole function. Somewhere, I have the pointers incorrectly used, can someone help me out here?
P.S.- I know templates are being used, I just learned to use them today, so I hope they are okay as well. The main problem is that the program is not working properly.
Could you be more specific as to what is wrong? And on line 77 and 87, it seems you are newing nodes without ever editing them, basically adding extra "empty" nodes to your tree. I would just remove line 77, as it seems to just create an error anyway; since your while loop operates on temp, and you have just created a new node for it to point to, anytime you add to the tree you are working on a new tree, so to speak, and end up with memory leaks all over the place.
Does your textbook go over how to create a linked list? Start with that and compare the explanations/code with your own.
In addition to the memory leak Zhuge points out, it looks like you are trying to add nodes to the list so they are in order. Make sure you change both the new node's next pointer, and the temp's pointer, like the code below...of course you'll have to test it out, but I think it will work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <class T>
void sortedList<T>::add(houses& x)
{
node *temp;
temp = head;
while(temp != NULL && greater == compare(x, temp->data))
{
temp = temp->next;
}
node *someNode = new node; //create the new node
someNode->data.title = x.title;
someNode->data.price = x.price;
someNode->next = temp->next; //set the new node's next pointer to the next in the list
temp->next = someNode; //set the temp's next pointer to the new node
}