Collection Classes in .net - the Queue
This is the seventh part of an article on collection classes in .net with C#. This part covers the Queue.
For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512
For the next part on the LinkedList see http://www.audacs.co.uk/ViewPage.aspx?PageID=520
The queue is very simillar to the stack. the key difference is that this is a FIFO structure. FIFO stands for first in first out. This class has been created to help us model real world scenarios where such behaviour is required. For example a printer queue.
Like the stack there are two versions of this class. The original version and a Generic version. These can be found at:
System.Collections.Queue
System.Collections.Generic.Queue
You can place any object at all in the queue since it is defined to hold a collection of objects. The generic versions of course also requires that you define this whereas the non generic version requires some casting within your code.
Lets have a look at a simple program which shows us how we can use the queue. Note that we use an Enqueue method to add something to the queue and we use a Dequeue method to remove an item.
Below we will show the code for a version using the original queue and then one using the generic version. The output from both which is shown afterwards is identical in both cases.
Original Version
Generic Version
So the only real difference is that we declare the queue as a generic type and then when we do a Dequeue then there is no casting involved.
The output from both programs looks like this:
And thats about it for the Queue. For the next part on the LinkedList see http://www.audacs.co.uk/ViewPage.aspx?PageID=520