15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

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

366<br />

Using Queues<br />

A queue gets its name because the data structure resembles a line of waiting<br />

people, such as a line at a bank teller. The first person in the line (queue) is<br />

also the first person who gets to leave the line. As a result, a queue is often a<br />

First In, First Out (FIFO) data structure, as shown in Figure 4-4.<br />

New data gets added to the end of the queue.<br />

Data #4<br />

Data #3 Data #2 Data #1<br />

Figure 4-4:<br />

The queue<br />

data<br />

structure<br />

mimics a<br />

line of<br />

people.<br />

Data #4 Data #3 Data #2 Data #1<br />

Data #4 Data #3 Data #2<br />

Data #1<br />

Old data gets removed from the front of the queue.<br />

Like a stack, a queue can expand and shrink automatically, depending on the<br />

amount of data you store in it. Unlike a stack that only lets you store and<br />

retrieve data from the top, a queue lets you store data on one end but<br />

remove that data from the opposite end.<br />

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

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

such as an array or a linked list, to create an abstract data structure.<br />

Fortunately, many <strong>programming</strong> language compilers come with libraries (or<br />

classes in object-oriented languages) of subprograms that have created the<br />

queue data structure <strong>for</strong> you.<br />

Because a queue is just a data structure, you can declare a variable to represent<br />

a queue in Visual Basic.NET by doing the following:<br />

Dim LongLine as New Queue<br />

This command simply identifies a LongLine variable as a queue data structure.<br />

The New command tells the computer to create a new stack.

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

Saved successfully!

Ooh no, something went wrong!