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

Create successful ePaper yourself

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

• Hav<strong>in</strong>g only a s<strong>in</strong>gle removal method, remove, is not actually a<br />

restriction, s<strong>in</strong>ce we can remove at the beg<strong>in</strong>n<strong>in</strong>g or end of a doubly l<strong>in</strong>ked list L<br />

by execut<strong>in</strong>g L.remove(L.getFirst()) or L.remove(L.getLast()),<br />

respectively.<br />

• Method toStr<strong>in</strong>g for convert<strong>in</strong>g an entire list <strong>in</strong>to a str<strong>in</strong>g is useful for<br />

test<strong>in</strong>g <strong>and</strong> debugg<strong>in</strong>g purposes.<br />

3.4 Circularly L<strong>in</strong>ked Lists <strong>and</strong> L<strong>in</strong>ked-List Sort<strong>in</strong>g<br />

In this section, we study some applications <strong>and</strong> extensions of l<strong>in</strong>ked lists.<br />

3.4.1 Circularly L<strong>in</strong>ked Lists <strong>and</strong> Duck, Duck, Goose<br />

The children's game, "Duck, Duck, Goose," is played <strong>in</strong> many cultures. Children <strong>in</strong><br />

M<strong>in</strong>nesota play a version called "Duck, Duck, Grey Duck" (but please don't ask us<br />

why.) In Indiana, this game is called "The Mosh Pot." And children <strong>in</strong> the Czech<br />

Republic <strong>and</strong> Ghana play s<strong>in</strong>g-song versions known respectively as "Pesek" <strong>and</strong><br />

"Antoakyire." A variation on the s<strong>in</strong>gly l<strong>in</strong>ked list, called the circularly l<strong>in</strong>ked list,<br />

is used for a number of applications <strong>in</strong>volv<strong>in</strong>g circle games, like "Duck, Duck,<br />

Goose." We discuss this type of list <strong>and</strong> the circle-game application next.<br />

A circularly l<strong>in</strong>ked list has the same k<strong>in</strong>d of nodes as a s<strong>in</strong>gly l<strong>in</strong>ked list. That is,<br />

each node <strong>in</strong> a circularly l<strong>in</strong>ked list has a next po<strong>in</strong>ter <strong>and</strong> a reference to an element.<br />

But there is no head or tail <strong>in</strong> a circularly l<strong>in</strong>ked list. For <strong>in</strong>stead of hav<strong>in</strong>g the last<br />

node's next po<strong>in</strong>ter be null, <strong>in</strong> a circularly l<strong>in</strong>ked list, it po<strong>in</strong>ts back to the first<br />

node. Thus, there is no first or last node. If we traverse the nodes of a circularly<br />

l<strong>in</strong>ked list from any node by follow<strong>in</strong>g next po<strong>in</strong>ters, we will cycle through the<br />

nodes.<br />

Even though a circularly l<strong>in</strong>ked list has no beg<strong>in</strong>n<strong>in</strong>g or end, we nevertheless need<br />

some node to be marked as a special node, which we call the cursor. The cursor<br />

node allows us to have a place to start from if we ever need to traverse a circularly<br />

l<strong>in</strong>ked list. And if we remember this start<strong>in</strong>g po<strong>in</strong>t, then we can also know when we<br />

are done-we are done with a traversal of a circularly l<strong>in</strong>ked list when we return to<br />

the node that was the cursor node when we started.<br />

We can then def<strong>in</strong>e some simple update methods for a circularly l<strong>in</strong>ked list:<br />

add(v): Insert a new node v immediately after the cursor; if the list is<br />

empty, then v becomes the cursor <strong>and</strong> its next po<strong>in</strong>ter po<strong>in</strong>ts to itself.<br />

remove(): Remove <strong>and</strong> return the node v immediately after the cursor<br />

(not the cursor itself, unless it is the only node); if the list becomes empty, the<br />

cursor is set to null.<br />

180

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

Saved successfully!

Ooh no, something went wrong!