5.7. List Element Implementations¶
5.7.1. List Element Implementations¶
When designing any container class, there are a number of design choices to be made regarding the data elements.
What to do if something can appear multiple times on a list? One option is to use a reference to elements. Another is to store separate copies. In general, the larger the elements and the more that they are duplicated, the more likely that pointers to shared elements is the better approach.
The next issue to consider is whether to enforce homogeneity in the list elements. That is, should lists be restricted so that all data elements stored are of the same object type? Or should it be possible to store different types?
In some applications, the designer would like to allow a given list
store elements with different types.
In Java, declaring the element to be of type
Object will stop the
compiler from enforcing any type restrictions.
In C++, a similar effect can be achieved by using
220.127.116.11. Element Deletion¶
Our last design issue is what to do to the list elements when the list itself is deleted? This is a serious concern in a language like C++ that does not support automatic garbage collection.