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

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 111Array-based lists are faster for r<strong>and</strong>om access by position. Positions can easilybe adjusted forwards or backwards by the next <strong>and</strong> prev methods. These oper<strong>at</strong>ionsalways take Θ(1) time. In contrast, singly linked lists have no explicit accessto the previous element, <strong>and</strong> access by position requires th<strong>at</strong> we march down thelist from the front (or the current position) to the specified position. Both of theseoper<strong>at</strong>ions require Θ(n) time in the average <strong>and</strong> worst cases, if we assume th<strong>at</strong>each position on the list is equally likely to be accessed on any call to prev ormoveToPos.Given a pointer to a suitable loc<strong>at</strong>ion in the list, the insert <strong>and</strong> removemethods for linked lists require only Θ(1) time. Array-based lists must shift the remainderof the list up or down within the array. This requires Θ(n) time in the average<strong>and</strong> worst cases. For many applic<strong>at</strong>ions, the time to insert <strong>and</strong> delete elementsdomin<strong>at</strong>es all other oper<strong>at</strong>ions. For this reason, linked lists are often preferred toarray-based lists.When implementing the array-based list, an implementor could allow the sizeof the array to grow <strong>and</strong> shrink depending on the number of elements th<strong>at</strong> areactually stored. This d<strong>at</strong>a structure is known as a dynamic array. Both the Java <strong>and</strong>C++/STL Vector classes implement a dynamic array. Dynamic arrays allow theprogrammer to get around the limit<strong>at</strong>ion on the st<strong>and</strong>ard array th<strong>at</strong> its size cannotbe changed once the array has been cre<strong>at</strong>ed. This also means th<strong>at</strong> space need notbe alloc<strong>at</strong>ed to the dynamic array until it is to be used. The disadvantage of thisapproach is th<strong>at</strong> it takes time to deal with space adjustments on the array. Each timethe array grows in size, its contents must be copied. A good implement<strong>at</strong>ion of thedynamic array will grow <strong>and</strong> shrink the array in such a way as to keep the overallcost for a series of insert/delete oper<strong>at</strong>ions rel<strong>at</strong>ively inexpensive, even though anoccasional insert/delete oper<strong>at</strong>ion might be expensive. A simple rule of thumb isto double the size of the array when it becomes full, <strong>and</strong> to cut the array size inhalf when it becomes one quarter full. To analyze the overall cost of dynamic arrayoper<strong>at</strong>ions over time, we need to use a technique known as amortized analysis,which is discussed in Section 14.3.4.1.4 Element Implement<strong>at</strong>ionsList users must decide whether they wish to store a copy of any given elementon each list th<strong>at</strong> contains it. For small elements such as an integer, this makessense. If the elements are payroll records, it might be desirable for the list nodeto store a reference to the record r<strong>at</strong>her than store a copy of the record itself. Thischange would allow multiple list nodes (or other d<strong>at</strong>a structures) to point to thesame record, r<strong>at</strong>her than make repe<strong>at</strong>ed copies of the record. Not only might thissave space, but it also means th<strong>at</strong> a modific<strong>at</strong>ion to an element’s value is autom<strong>at</strong>icallyreflected <strong>at</strong> all loc<strong>at</strong>ions where it is referenced. The disadvantage of storing apointer to each element is th<strong>at</strong> the pointer requires space of its own. If elements are

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

Saved successfully!

Ooh no, something went wrong!