11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesnever duplic<strong>at</strong>ed, then this additional space adds unnecessary overhead. Java mostn<strong>at</strong>urally stores references to objects, meaning th<strong>at</strong> only a single copy of an objectsuch as a payroll record will be maintained, even if it is on multiple lists.Whether it is more advantageous to use references to shared elements or separ<strong>at</strong>ecopies depends on the intended applic<strong>at</strong>ion. In general, the larger the elements<strong>and</strong> the more they are duplic<strong>at</strong>ed, the more likely th<strong>at</strong> references to shared elementsis the better approach.A second issue faced by implementors of a list class (or any other d<strong>at</strong>a structureth<strong>at</strong> stores a collection of user-defined d<strong>at</strong>a elements) is whether the elements storedare all required to be of the same type. This is known as homogeneity in a d<strong>at</strong>astructure. In some applic<strong>at</strong>ions, the user would like to define the class of the d<strong>at</strong>aelement th<strong>at</strong> is stored on a given list, <strong>and</strong> then never permit objects of a differentclass to be stored on th<strong>at</strong> same list. In other applic<strong>at</strong>ions, the user would like topermit the objects stored on a single list to be of differing types.For the list implement<strong>at</strong>ions presented in this section, the compiler requires th<strong>at</strong>all objects stored on the list be of the same type. Besides Java generics, there areother techniques th<strong>at</strong> implementors of a list class can use to ensure th<strong>at</strong> the elementtype for a given list remains fixed, while still permitting different lists to storedifferent element types. One approach is to store an object of the appropri<strong>at</strong>e typein the header node of the list (perhaps an object of the appropri<strong>at</strong>e type is suppliedas a parameter to the list constructor), <strong>and</strong> then check th<strong>at</strong> all insert oper<strong>at</strong>ions onth<strong>at</strong> list use the same element type.The third issue th<strong>at</strong> users of the list implement<strong>at</strong>ions must face is primarily ofconcern when programming in languages th<strong>at</strong> do not support autom<strong>at</strong>ic garbagecollection. Th<strong>at</strong> is how to deal with the memory of the objects stored on the listwhen the list is deleted or the clear method is called. The list destructor <strong>and</strong> theclear method are problem<strong>at</strong>ic in th<strong>at</strong> there is a potential th<strong>at</strong> they will be misused.Deleting listArray in the array-based implement<strong>at</strong>ion, or deleting a link nodein the linked list implement<strong>at</strong>ion, might remove the only reference to an object,leaving its memory space inaccessible. Unfortun<strong>at</strong>ely, there is no way for the listimplement<strong>at</strong>ion to know whether a given object is pointed to in another part of theprogram or not. Thus, the user of the list must be responsible for deleting theseobjects when th<strong>at</strong> is appropri<strong>at</strong>e.4.1.5 Doubly Linked ListsThe singly linked list presented in Section 4.1.2 allows for direct access from alist node only to the next node in the list. A doubly linked list allows convenientaccess from a list node to the next node <strong>and</strong> also to the preceding node on the list.The doubly linked list node accomplishes this in the obvious way by storing twopointers: one to the node following it (as in the singly linked list), <strong>and</strong> a secondpointer to the node preceding it. The most common reason to use a doubly linked

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!