0

I am making a simple linked list with an insert function that has 4 conditions:

  1. add in the middle of the list
  2. add to the head if the list is not empty

  3. add to an empty list

  4. add to the end of a list

I have made an addNode function that takes care of the last two conditions and im trying to implement it inside of the insertNode but it keeps crashing? is there any specific reason or am I just missing something simple?

addNode:

void linkList::addNode(int dataAdd) // this only adds a node to the end
{
    nodePtr n = new node; // what this does is make the node pointer 'n' point to this new node
    n->next = NULL;
    n->data = dataAdd;

    if (head != NULL)
    {
        follow = head;
        while (follow->next != NULL)
        {
            follow = follow->next;
        }
        follow->next = n;
    }
    else
    {
        head = n;
    }

}

insertNode:

void linkList::insertNode(int dataInsert)
{
    nodePtr n = new node;
    n->next = NULL;
    n->data = dataInsert;

    follow = head;

    while (follow != NULL)
    {
        trail = follow;
        follow = follow->next;

        if (head->data < dataInsert){
            n->next = head; //n's next will become the current head (n is now the first item in the linkList)
            head = n;       //n the head structure will now be n (n is the new head)
        }

        else if (follow->data < dataInsert)
        {

            trail->next = n;
            n->next = follow;
            break;
        }
        else
        {
            addNode(dataInsert);
        }

    }
}

I want to call addNode in the else statement in insertNode

  • 5
    Yes, your missing something simple: simply use a debugger, to step through your code one line at a time and examine all the variables, pointers, etc, at each step of the way. It should be fairly simply to determine what went wrong, and how to fix it. – Sam Varshavchik Sep 30 '16 at 01:54
  • `follow->next` is assigned to `follow` without checking any values. Could this cause `follow` to be NULL in the `else if` in insertNode? – Robert Prévost Sep 30 '16 at 02:03
  • @SamVarshavchik thank you, I tried that, otherwise I wouldn't be on here asking this question. – djdangerousdick Sep 30 '16 at 02:42
  • 1
    Glad to hear that you've tried using a debugger. Now, what observations have you made, using your debugger? – Sam Varshavchik Sep 30 '16 at 02:44
  • @SamVarshavchik I mean i am kind of an amateur at using the debugger but I see that a pointer is placed inside of the value for head instead of a integer value once the program enters the addNode function called from insertNode? – djdangerousdick Sep 30 '16 at 02:46
  • "pointer is placed inside of the value fo rthe head instead of an integer value" are all valid English words, but put together they don't really make much of a sense. [Maybe you should try to talk to your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging), in order to make sense of it. – Sam Varshavchik Sep 30 '16 at 02:50
  • Please, can you provide a minimal code that reproduces the issue ? For now, an advice about your code : manual (des)allocation is a pain that you can avoid by using [smart pointers](http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one). And use `nullptr` instead of `NULL`. – informaticienzero Sep 30 '16 at 07:53

0 Answers0