Code Wont Finish Running! Help!

Null
Last edited on
Visual Studio shows up no errors


what is the insert method you're trying to call? Doesn't compile for me (VS for me as well).
Im not quite sure, I worked with my tutor and this is what he came up with. The insertnode handles inserting value m into a node which is passed from the user, all values get eventually sorted into a list.
insert() call at line 47 and 58 should have been insertnode(). If that call is insertnode(), then I have found some problem with your implementation with it.

1
2
3
4
5
6
7
void insertnode(nodeptr& head, double m){
    nodeptr p = head;
    nodeptr k = p;
    if (!p){
       nodeptr n = new node(m, NULL);
    }
    ...


You should do something with new node pointed to by variable n in line 5 or else it will cause memory leak and that new node will not be inserted to the list.
Can I just remove that? And leave the else condition?

**UPDATE**

I ran a debugger and an error popped up

" First-chance exception at 0x002189C6 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x002189C6 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000000."

And it points to line 18
Last edited on
chance is the variable "head" is NULL at some point
In which function?
That if statement must not be removed since it will handle the case that head is NULL or in the case for first insert. You cannot do 2nd and succeeding insert if you can't fix the case of the first insert. What I said you must do something is that, perhaps assign that new node pointed to by n to head.

Do you understand what this insertnode() does? I suggest you understand it step by step since you can't understand what other people are suggesting here.
Topic archived. No new replies allowed.