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.6 Exercises 139(b) L2.append(10);L2.append(20);L2.append(15);L2.moveToStart();L2.insert(39);L2.next();L2.insert(12);4.3 Write a series of Java st<strong>at</strong>ements th<strong>at</strong> uses the List ADT of Figure 4.1 tocre<strong>at</strong>e a list capable of holding twenty elements <strong>and</strong> which actually stores thelist with the following configur<strong>at</strong>ion:〈 2, 23 | 15, 5, 9 〉.4.4 Using the list ADT of Figure 4.1, write a function to interchange the currentelement <strong>and</strong> the one following it.4.5 In the linked list implement<strong>at</strong>ion presented in Section 4.1.2, the current positionis implemented using a pointer to the element ahead of the logicalcurrent node. The more “n<strong>at</strong>ural” approach might seem to be to have currpoint directly to the node containing the current element. However, if thiswas done, then the pointer of the node preceding the current one cannot beupd<strong>at</strong>ed properly because there is no access to this node from curr. Analtern<strong>at</strong>ive is to add a new node after the current element, copy the value ofthe current element to this new node, <strong>and</strong> then insert the new value into theold current node.(a) Wh<strong>at</strong> happens if curr is <strong>at</strong> the end of the list already? Is there still away to make this work? Is the resulting code simpler or more complexthan the implement<strong>at</strong>ion of Section 4.1.2?(b) Will deletion always work in constant time if curr points directly tothe current node? In particular, can you make several deletions in arow?4.6 Add to the LList class implement<strong>at</strong>ion a member function to 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 to merge two linked lists. The input lists have their elementsin sorted order, from lowest to highest. The output list should also be sortedfrom lowest to highest. 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 to the first link node of the list. This can be useful whenyou wish to have a rel<strong>at</strong>ive positioning for elements, but no concept of anabsolute first or last position.

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

Saved successfully!

Ooh no, something went wrong!