Having introduced linked lists in our previous post, let us look at some operations on such a data structure. Specifically, let us look at how to insert a new item (a new node) to a linked list. Typically, we start with an empty linked list (denoted by just one node, the NULL node) and grow it by adding more and more items. The usual approach to adding a new item is to form a node (the building block) for the new item and then attach the item to a position in the linked list - by default we choose to add it to the head.

Node *LinkedList::insert_item (int item) {
  Node *new_node = new Node(item); // form the building block
  new_node->next = this->head;     // set the next pointer
  this->head = new_node;           // make new node the head
  this->length++;                  // update the length
}