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.

368<br />

Using Queues<br />

Removing data from a queue<br />

You always remove data from a queue by taking that data off the front of the<br />

queue. The front of the queue always contains the data that’s been stored in<br />

the queue the longest.<br />

In Visual Basic.NET, you can remove and retrieve data off a queue by using<br />

the Dequeue command, as shown in the following Visual Basic.NET example:<br />

Dim LongLine as New Queue<br />

Dim X as Object<br />

LongLine.Enqueue (“My cat”)<br />

LongLine.Enqueue (108.75)<br />

LongLine.Enqueue (“Fat dog”)<br />

X = LongLine.Dequeue<br />

As an alternative to removing data from a queue, you can retrieve data by<br />

using the Peek command. To use the Peek command, you have to assign the<br />

value of the Peek command to a variable like this:<br />

Dim LongLine as New Queue<br />

Dim X as Object<br />

LongLine.Enqueue (“Tasha Korat”)<br />

LongLine.Enqueue (7.25)<br />

LongLine.Enqueue (“Gray”)<br />

X = LongLine.Peek<br />

The preceding code assigns the value “Tasha Korat” to the X variable,<br />

which is declared as an Object data type. (In Visual Basic.NET, an Object<br />

data type can hold any type of data including integers, strings, and decimal<br />

numbers, such as 57.98).<br />

The Peek command only retrieves the data but leaves it at the front of the<br />

queue. Figure 4-6 shows the difference between the Peek and the Dequeue<br />

commands.<br />

Counting and searching a queue<br />

Because a queue can keep growing each time you add more data to it, most<br />

<strong>programming</strong> languages provide a way to count the total number of items<br />

currently stored in the queue.

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

Saved successfully!

Ooh no, something went wrong!