#include class ListElem { public: int value; // Integer ListElem *next; // Pointer used to make link }; void Print(ListElem *head) { ListElem *p; p = head; // Very important: start at first element cout << "List ==> "; while ( p != NULL ) { cout << p->value << " "; // Access value stored in list element p = p->next; // Visit next element in list } cout << "\n\n"; } /* ----------------------------------------------------------- InsertAtHead: insert elem at start of linked list Question: what would happen if we pass "head" by value ? ----------------------------------------------------------- */ void InsertAtHead(ListElem * & head, ListElem * elem) { elem->next = head; // Make new list element points // to the start of "old list" head = elem; // Make "head" points to // the **new** starting object } int main(int argc, char *argv[]) { ListElem *head, *p; void Print(ListElem *); void InsertAtHead(ListElem * &, ListElem *); head = NULL; Print(head); p = new ListElem; p->value = 1500; InsertAtHead(head, p); Print(head); p = new ListElem; p->value = 2500; InsertAtHead(head, p); Print(head); p = new ListElem; p->value = 3500; InsertAtHead(head, p); Print(head); }