Looks like a linked list.
Linked Lists are ways to store groups of data so that an element can easily be added/removed anywhere in the list. The idea is, each 'Node' contains 2 things:
1) Data (ie, the information you actually want to store in the list)
2) A pointer to the next element in the list.
So if you have 3 elements, A B and C... A's pointer points to B, and B's pointer points to C.
The "head" is the start of the list. IE: the pointer to the first element in the list.
Your push() function is adding a new element to the head of the list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void push(DT val)
{
// the goal is to put the 'val' data at the start/head of this linked list.
// In order to do this, we create a new node for the data to rest in:
Node<DT> * temp = new Node<DT>();
// once we have the node 'temp', we put 'val' in it as the data it is to hold:
temp->data = val;
// Now we set its pointer to point to the next element in the list. Since
// This node is going to be the new head, so the next node is the old head:
temp->next = head;
// Set the new head to this node:
head = temp;
}
| |
Basically... how it works is...
head
is the first element in the list...
head->next
is the 2nd,
head->next->next
is the 3rd, and so on and so on.