C++20 sections · 1024 units
Open in Course

Node Creation - The Idea

Building chains

This self-referential structure lets you build chains of arbitrary length. The last node's next pointer is nullptr, marking the end of the chain.

To build a linked list, keep track of the head (first node) and the tail (last node). When adding a new node, create it with new, set its next to nullptr, then update the tail's next to point to it.

Finally, update tail to the new node. To traverse a list, start at head and follow next pointers until you reach nullptr. Use a loop: while (current != nullptr) { process current->data; current = current->next; }.

This visits every node exactly once.