15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

370<br />

Using Deques<br />

The second Contains command looks <strong>for</strong> the “Orange juice” string.<br />

Because this string isn’t stored in the stack, this Contains command<br />

returns a False value, which is stored in the Y variable.<br />

The Contains command just tells you whether a chunk of data is in a<br />

queue, but it doesn’t tell you where in the queue that data might be.<br />

Using Deques<br />

A queue only lets you add data on one end of the data structure and remove<br />

data from the opposite end. A deque (pronounced deck) acts like a queue<br />

that lets you add or remove data from either end, as shown in Figure 4-7.<br />

A deque allows you to add or remove data<br />

from both ends of the data structure.<br />

Figure 4-7:<br />

A deque<br />

acts like a<br />

two-way<br />

queue.<br />

New data<br />

1 2<br />

Data #4 Data #3<br />

3 4<br />

Data #2 Data #1 New data<br />

Most <strong>programming</strong> languages don’t offer the deque data structure as a builtin<br />

feature. Instead, you have to create a queue with other data structures,<br />

such as a linked list, and then write code to manage the storage and removal<br />

of data from both ends of the deque.<br />

A deque is basically a linked list of nodes that contain data and two pointers:<br />

✦ One pointer points to the previous node.<br />

✦ The second pointer points to the next node, as shown in Figure 4-8.<br />

Figure 4-8:<br />

Each node<br />

in a deque<br />

contains two<br />

pointers that<br />

point to the<br />

next and previous<br />

node.<br />

A double-linked list consists of nodes that hold data and two<br />

pointers that point to the next and previous nose.<br />

Data Data Data Data

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

Saved successfully!

Ooh no, something went wrong!