23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

Show that, us<strong>in</strong>g an extendable array that grows <strong>and</strong> shr<strong>in</strong>ks as described <strong>in</strong> the<br />

previous exercise, the follow<strong>in</strong>g series of 2n operations takes O(n) time: (i) n<br />

push operations on an array list with <strong>in</strong>itial capacity N = 1; (ii) n pop (removal<br />

of the last element) operations.<br />

C-6.4<br />

Show how to improve the implementation of method add <strong>in</strong> Code Fragment 6.3<br />

so that, <strong>in</strong> case of an overflow, the elements are copied <strong>in</strong>to their f<strong>in</strong>al place <strong>in</strong><br />

the new array, that is, no shift<strong>in</strong>g should be done <strong>in</strong> this case.<br />

C-6.5<br />

Consider an implementation of the array list ADT us<strong>in</strong>g an extendable array, but<br />

<strong>in</strong>stead of copy<strong>in</strong>g the elements of the array list <strong>in</strong>to an array of double the size<br />

(that is, from N to 2N) when its capacity is reached, we copy the elements <strong>in</strong>to<br />

an array with N/4 additional cells, go<strong>in</strong>g from capacity N to N + N/4.<br />

Show that perform<strong>in</strong>g a sequence of n push operations (that is, <strong>in</strong>sertions at the<br />

end) still runs <strong>in</strong> O(n) time <strong>in</strong> this case.<br />

C-6.6<br />

The NodePositionList implementation given <strong>in</strong> Code Fragments 6.9-6.11<br />

does not do any error checks to test if a given position p is actually a member of<br />

this particular list. For example, if p is a position <strong>in</strong> list S <strong>and</strong> we call<br />

T.addAfter(p, e) on a different list T, then we actually will add the<br />

element to S just after p. Describe how to change the NodePositionList<br />

implementation <strong>in</strong> an efficient manner to disallow such misuses.<br />

C-6.7<br />

Suppose we want to extend the Sequence abstract data type with methods<br />

<strong>in</strong>dexOfElement(e) <strong>and</strong> positionOfElement(e), which respectively<br />

return the <strong>in</strong>dex <strong>and</strong> the position of the (first occurrence of) element e <strong>in</strong> the<br />

sequence. Show how to implement these methods by express<strong>in</strong>g them <strong>in</strong> terms<br />

of other methods of the Sequence <strong>in</strong>terface.<br />

C-6.8<br />

Give an adaptation of the array list ADT to the deque ADT that is different from<br />

that given <strong>in</strong> Table 6.1.<br />

C-6.9<br />

Describe the structure <strong>and</strong> pseudo-code for an array-based implementation of<br />

the array list ADT that achieves O(1) time for <strong>in</strong>sertions <strong>and</strong> removals at <strong>in</strong>dex<br />

0, as well as <strong>in</strong>sertions <strong>and</strong> removals at the end of the array list. Your<br />

implementation should also provide for a constant-time get method. (H<strong>in</strong>t:<br />

368

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

Saved successfully!

Ooh no, something went wrong!