In the
print()
member function, an auxiliary pointer
temp
will be used to chain down the list. It is initialized to the address of the
slist
head
h
. The pointer
h
cannot be used because its value would be lost, in effect destroying access to the list.
void slist::print() const //object unchanged
{
slistelem* temp = h;
while (temp != 0) { //detect end slist
cout << temp -> data << " -> ";
temp = temp -> next;
}
cout << "\n###" << endl;
}
The value 0 in the
while
loop represents the end-of-list value. It is guaranteed to be such because the constructor
slist::slist()
initialized it, and the
slist::prepend()
function maintains it as the end-of-list pointer value. Notice that the internals of this loop could be changed to process the entire list in some other manner.