| ||||
Copyright 1999 Sams Publishing |
|
How a FIFO Queue Works |
As elements are added to a FIFO queue, they are stored in order. When elements are removed from a FIFO queue, they come out in exactly the same order as they were added. The first element put into the queue will be the first element taken out of the queue, the second element added to the queue will be the second element removed from the queue, and so on. |
Figure 18.1 shows an example of how elements are added to a FIFO queue. In the first step, item A is added to an empty FIFO queue. In the second step, you can see that item A is at the bottom (where it will eventually be removed) and that item B is being added. In the third step, items A and B are in the queue, and item C is being added. The result is that items A, B, and C are inside the queue and are stacked in the order they were added. |
Figure 18.1: Adding items to a FIFO queue. | |
Figure 18.2 continues this example. In the first step, you can see that items A, B, and C are in the queue and that item A is about to be removed. In the second step, item A is gone, and items B and C have shifted down. When item D is added, it ends up behind item C. In the third step, items B, C, and D are in the FIFO queue, and item B is being removed. The fourth step shows only items C and D remaining; item C is about to be removed. The result is that only item D remains inside the queue, and items A, B, and C were removed in the same order as they were added. If another element were to be removed, it would be item D, after which the queue would be empty again. |
Figure 18.2: Removing items from and adding items to a FIFO queue. | |
| |||
Toc |