next up previous index
Next: The ListNode Up: Linked Implementations Previous: The Stack as a   Index

A List Node Class

We need to store the stack's data items in a linearly ordered fashion. This was easy with an array, because stack elements are already ordered by their array addresses. If we are only going to allocate enough storage for a single element at a time, we need to create the ordering ourselves.

The simplest approach is to store two things when we push a new element onto the stack. We must store not only the new data item, but also a reference to the item that was previously at the top of the stack, so that, when we pop the first item, we know what we should be left with. Of course that previous item would have been stored in the same way, with a reference to the one before that, and so on. So we need some way of telling that the list is terminated. The conventional approach is to use the null reference, playing the role of the empty list.



Subsections

Peter Williams 2005-06-07