I'm working with linked lists for my first time and have to create a function that can insert a node at the end of a doubly linked list. So far I have
void LinkedList::insertAtTail(const value_type& entry) {
Node *newNode = new Node(entry, NULL, tail);
tail->next = newNode;
tail = newNode;
++node_count;
}
The Node class accepts a value to be stored, a value for the next pointer to point to, and a value for the previous pointer in that order. Whenever I try to insert a node here, I get an error saying there was an unhandled exception and there was an access violation in writing to location 0x00000008.
I'm not entirely sure what's going wrong here but I assume it has something to do with dereferencing a null pointer based on the error message. I would really appreciate some help with solving this problem.
EDIT:
I should have clarified early, tail is a pointer that points to the last node in the list. Tail->next accesses the next variable of that last node which, before the function runs, points to NULL but after it executes should point to the new node created.
LinkedList
andNode
classes, as there's not much context in your first post. - Thomas Matthewstail
andtail->next
pointing to thenewNode
? (Looks like a circular reference, but I could be wrong.) - Thomas Matthewstail
initially NULL? You can't dereference it intail->next
until it already points to the first element - Jonathan Wakelyif (tail)
in front of thattail->next
assignment. Likewise, where's the head-assignment on the chance this list is purely empty and the tail insert is the first one?? might want that as well. - WhozCraig