I'm playing around with a linked list, and I'm getting an error when I try to print out the value of the tail and the address of the next value:
struct Node {
int n;
Node *next;
};
class LinkedList {
public:
Node *head = NULL;
Node *tail = NULL;
};
int main() {
LinkedList L;
L.head = NULL;
L.tail = NULL;
Node *new_node = new Node();
new_node->n = 1;
new_node->next = NULL;
L.tail->next = new_node;
L.tail = new_node;
cout << L.tail << endl;
cout << L.tail->next << endl;
}
nullptrin preference to C's typelessNULL.L.tail->nextis set null, so you're asking it to print an invalidNodepointer.L.tail->next = new_node;is dereferencing a null pointer. I would expect this to go splat.