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...

Create successful ePaper yourself

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

118 Chap. 4 Lists, Stacks, <strong>and</strong> QueuesArray-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 operationsalways take Θ(1) time. In contrast, singly linked lists have no explicit access<strong>to</strong> the previous element, <strong>and</strong> access by position requires that we march down thelist from the front (or the current position) <strong>to</strong> the specified position. Both of theseoperations require Θ(n) time in the average <strong>and</strong> worst cases, if we assume thateach position on the list is equally likely <strong>to</strong> be accessed on any call <strong>to</strong> prev ormoveToPos.Given a pointer <strong>to</strong> a suitable location 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 applications, the time <strong>to</strong> insert <strong>and</strong> delete elementsdominates all other operations. For this reason, linked lists are often preferred <strong>to</strong>array-based lists.When implementing the array-based list, an implemen<strong>to</strong>r could allow the sizeof the array <strong>to</strong> grow <strong>and</strong> shrink depending on the number of elements that are actuallys<strong>to</strong>red. This data structure is known as a dynamic array. For example, theJava Vec<strong>to</strong>r class implements a dynamic array. Dynamic arrays allow the programmer<strong>to</strong> get around the limitation on the st<strong>and</strong>ard array that its size cannot bechanged once the array has been created. This also means that space need not beallocated <strong>to</strong> the dynamic array until it is <strong>to</strong> be used. The disadvantage of this approachis that it takes time <strong>to</strong> deal with space adjustments on the array. Each timethe array grows in size, its contents must be copied. A good implementation of thedynamic array will grow <strong>and</strong> shrink the array in such a way as <strong>to</strong> keep the overallcost for a series of insert/delete operations relatively inexpensive, even though anoccasional insert/delete operation might be expensive. To analyze the cost of dynamicarray operations, we need <strong>to</strong> use a technique known as amortized analysis,which is discussed in Section 14.3.4.1.4 Element ImplementationsList users must decide whether they wish <strong>to</strong> s<strong>to</strong>re a copy of any given elemen<strong>to</strong>n each list that contains it. For small elements such as an integer, this makessense. If the elements are payroll records, it might be desirable for the list node<strong>to</strong> s<strong>to</strong>re a reference <strong>to</strong> the record rather than s<strong>to</strong>re a copy of the record itself. Thischange would allow multiple list nodes (or other data structures) <strong>to</strong> point <strong>to</strong> thesame record, rather than make repeated copies of the record. Not only might thissave space, but it also means that a modification <strong>to</strong> an element’s value is au<strong>to</strong>maticallyreflected at all locations where it is referenced. The disadvantage of s<strong>to</strong>ring a

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

Saved successfully!

Ooh no, something went wrong!