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.

148 Chap. 4 Lists, Stacks, <strong>and</strong> Queues4.5 In the linked list implementation presented in Section 4.1.2, the current positionis implemented using a pointer <strong>to</strong> the element ahead of the logicalcurrent node. The more “natural” approach might seem <strong>to</strong> be <strong>to</strong> have currpoint directly <strong>to</strong> the node containing the current element. However, if thiswas done, then the pointer of the node preceding the current one cannot beupdated properly because there is no access <strong>to</strong> this node from curr. Analternative is <strong>to</strong> add a new node after the current element, copy the value ofthe current element <strong>to</strong> this new node, <strong>and</strong> then insert the new value in<strong>to</strong> theold current node.(a) What happens if curr is at the end of the list already? Is there still away <strong>to</strong> make this work? Is the resulting code simpler or more complexthan the implementation of Section 4.1.2?(b) Will deletion always work in constant time if curr points directly <strong>to</strong>the current node?4.6 Add <strong>to</strong> the LList class implementation a member function <strong>to</strong> reverse theorder of the elements on the list. Your algorithm should run in Θ(n) time fora list of n elements.4.7 Write a function <strong>to</strong> merge two linked lists. The input lists have their elementsin sorted order, from smallest <strong>to</strong> highest. The output list should also be sortedfrom highest <strong>to</strong> lowest. Your algorithm should run in linear time on the lengthof the output list.4.8 A circular linked list is one in which the next field for the last link nodeof the list points <strong>to</strong> the first link node of the list. This can be useful whenyou wish <strong>to</strong> have a relative positioning for elements, but no concept of anabsolute first or last position.(a) Modify the code of Figure 4.8 <strong>to</strong> implement circular singly linked lists.(b) Modify the code of Figure 4.14 <strong>to</strong> implement circular doubly linkedlists.4.9 Section 4.1.3 states “the space required by the array-based list implementationis Ω(n), but can be greater.” Explain why this is so.4.10 Section 4.1.3 presents an equation for determining the break-even point forthe space requirements of two implementations of lists. The variables are D,E, P , <strong>and</strong> n. What are the dimensional units for each variable? Show thatboth sides of the equation balance in terms of their dimensional units.4.11 Use the space equation of Section 4.1.3 <strong>to</strong> determine the break-even point foran array-based list <strong>and</strong> linked list implementation for lists when the sizes forthe data field, a pointer, <strong>and</strong> the array-based list’s array are as specified.

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

Saved successfully!

Ooh no, something went wrong!