12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 4.1 Lists 119pointer <strong>to</strong> each element is that the pointer requires space of its own. If elements arenever duplicated, then this additional space adds unnecessary overhead. Java mostnaturally s<strong>to</strong>res references <strong>to</strong> objects, meaning that 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 <strong>to</strong> use references <strong>to</strong> shared elements or separatecopies depends on the intended application. In general, the larger the elements<strong>and</strong> the more they are duplicated, the more likely that references <strong>to</strong> shared elementsis the better approach.A second issue faced by implemen<strong>to</strong>rs of a list class (or any other data structurethat s<strong>to</strong>res a collection of user-defined data elements) is whether the elements s<strong>to</strong>redare all required <strong>to</strong> be of the same type. This is known as homogeneity in a datastructure. In some applications, the user would like <strong>to</strong> define the class of the dataelement that is s<strong>to</strong>red on a given list, <strong>and</strong> then never permit objects of a differentclass <strong>to</strong> be s<strong>to</strong>red on that same list. In other applications, the user would like <strong>to</strong>permit the objects s<strong>to</strong>red on a single list <strong>to</strong> be of differing types.For the list implementations presented in this section, the compiler requires thatall objects s<strong>to</strong>red on the list be of the same type. Besides Java generics, there areother techniques that implemen<strong>to</strong>rs of a list class can use <strong>to</strong> ensure that the elementtype for a given list remains fixed, while still permitting different lists <strong>to</strong> s<strong>to</strong>redifferent element types. One approach is <strong>to</strong> s<strong>to</strong>re an object of the appropriate typein the header node of the list (perhaps an object of the appropriate type is suppliedas a parameter <strong>to</strong> the list construc<strong>to</strong>r), <strong>and</strong> then check that all insert operations onthat list use the same element type.The third issue that users of the list implementations must face is primarily ofconcern when programming in languages that do not support au<strong>to</strong>matic garbagecollection. That is how <strong>to</strong> deal with the memory of the objects s<strong>to</strong>red on the listwhen the list is deleted or the clear method is called. The list destruc<strong>to</strong>r <strong>and</strong>the clear method are problematic in that there is a potential that they will bemisused. Deleting listArray in the array-based implementation, or deleting alink node in the linked list implementation, might remove the only reference <strong>to</strong> anobject, leaving its memory space inaccessible. Unfortunately, there is no way forthe list implementation <strong>to</strong> know whether a given object is pointed <strong>to</strong> in another par<strong>to</strong>f the program or not. Thus, the user of the list must be responsible for deletingthese objects when that is appropriate.4.1.5 Doubly Linked ListsThe singly linked list presented in Section 4.1.2 allows for direct access from alist node only <strong>to</strong> the next node in the list. A doubly linked list allows convenient

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

Saved successfully!

Ooh no, something went wrong!