-1

so I implemented the definition for a function insertEnd which inserts a linked list node at the end of a linked list. For the most part, it seems to work on its own, but I seem to have issues when using it in other functions (such as concatenating two linked lists) and nothing shows up on the console when it is called so I can't even use a breakpoint to debug

template <class Object>
void List<Object>::insertEnd(const Object& data) // INSERT: At the end!
{
    ListNode<Object> *getToEnd = head;
    while (getToEnd->getNext() != nullptr)
        getToEnd = getToEnd->getNext();
    ListNode<Object>* newnode = new ListNode<Object>(data, NULL);
    getToEnd->setNext(newnode);
}

EDIT: Here's what I'm trying to use insertEnd with (a function that concactenates)enter image description here

What I initially did was use a different insert that inserted at the beginning of a linked list, which worked, but that isn't what this new overloaded function is suppose to do (it's suppose to append one list to the back of another), so I created insertEnd to try to use it.

Crowning
  • 167
  • 1
  • 2
  • 10
  • 1
    Nothing looks particularly wrong, as long as `head` can't ever be `nullptr`. Perhaps there are problems with your use of it? (Nothing *should* show up in the console when it is called, and that's completely unrelated to breakpoints.) – molbdnilo Jul 22 '15 at 12:13
  • Can you please elaborate what is the second parameter that is passed as NULL in the constructor of ListNode – Itban Saeed Jul 22 '15 at 12:13
  • It's possible there are bugs in the rest of the code, that we cannot see here. This means the list would be in a strange state, and therefore we can't reason about what this `insertEnd` would do. – Aaron McDaid Jul 22 '15 at 12:13
  • You might need to think about what happens if the list is empty. Do you *intend* that an empty list would have NULL in `head`? If so, then that is one of the problems you'll have to work on – Aaron McDaid Jul 22 '15 at 12:15
  • Also, mixing up of `NULL` and `nullptr` may unknowingly create issues. Use `nullptr` everywhere. – Shreevardhan Jul 22 '15 at 12:17
  • @molbdnilo , I meant that nothing shows up when a cout message should show up after I click 'a' or "A' to call the concatenate insert function. Normally, the main menu cout message pops up again but it doesn't http://puu.sh/j9c5O/a07508e290.png – Crowning Jul 22 '15 at 23:34
  • @Itban Saeed it's ListNode( const Object& theElement, ListNode * node ) – Crowning Jul 22 '15 at 23:35

1 Answers1

0

It is unknown how the list is defined and initialized. Nevertheless it seems you should check whether head is equal to NULL.

For example

template <class Object>
void List<Object>::insertEnd(const Object& data) // INSERT: At the end!
{
    ListNode<Object>* newnode = new ListNode<Object>( data, nullptr );

    if ( head == nullptr )
    {
        head = newnode;
    }
    else
    {
        ListNode<Object> *getToEnd = head;
        while ( getToEnd->getNext() != nullptr ) getToEnd = getToEnd->getNext();

        getToEnd->setNext(newnode);
    }
}

And head should be initialized to nullptr then the list is created.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335