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.

Figure 3.9:<br />

Sample output of a Tic-Tac-Toe game.<br />

3.2 S<strong>in</strong>gly L<strong>in</strong>ked Lists<br />

In the previous sections, we presented the array data structure <strong>and</strong> discussed some of<br />

its applications. Arrays are nice <strong>and</strong> simple for stor<strong>in</strong>g th<strong>in</strong>gs <strong>in</strong> a certa<strong>in</strong> order, but<br />

they have the drawback of not be<strong>in</strong>g very adaptable, s<strong>in</strong>ce we have to fix the size N of<br />

the array <strong>in</strong> advance.<br />

There are other ways to store a sequence of elements, however, that do not have this<br />

drawback. In this section, we explore an important alternate implementation, which is<br />

known as the s<strong>in</strong>gly l<strong>in</strong>ked list.<br />

A l<strong>in</strong>ked list, <strong>in</strong> its simplest form, is a collection of nodes that together form a l<strong>in</strong>ear<br />

order<strong>in</strong>g. The order<strong>in</strong>g is determ<strong>in</strong>ed as <strong>in</strong> the children's game "Follow the Leader,"<br />

<strong>in</strong> that each node is an object that stores a reference to an element <strong>and</strong> a reference,<br />

called next, to another node. (See Figure 3.10.)<br />

Figure 3.10: Example of a s<strong>in</strong>gly l<strong>in</strong>ked list whose<br />

elements are str<strong>in</strong>gs <strong>in</strong>dicat<strong>in</strong>g airport codes. The next<br />

po<strong>in</strong>ters of each node are shown as arrows. The null<br />

object is denoted as .<br />

It might seem strange to have a node reference another node, but such a scheme easily<br />

works. The next reference <strong>in</strong>side a node can be viewed as a l<strong>in</strong>k or po<strong>in</strong>ter to<br />

another node. Likewise, mov<strong>in</strong>g from one node to another by follow<strong>in</strong>g a next<br />

reference is known as l<strong>in</strong>k hopp<strong>in</strong>g or po<strong>in</strong>ter hopp<strong>in</strong>g. The first <strong>and</strong> last node of a<br />

l<strong>in</strong>ked list usually are called the head <strong>and</strong> tail of the list, respectively. Thus, we can<br />

l<strong>in</strong>k hop through the list start<strong>in</strong>g at the head <strong>and</strong> end<strong>in</strong>g at the tail. We can identify the<br />

tail as the node hav<strong>in</strong>g a null next reference, which <strong>in</strong>dicates the end of the list. A<br />

l<strong>in</strong>ked list def<strong>in</strong>ed <strong>in</strong> this way is known as a s<strong>in</strong>gly l<strong>in</strong>ked list.<br />

164

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

Saved successfully!

Ooh no, something went wrong!