School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,703 people online right now. Registration is fast and FREE... Join Now!




Introduction to Generic Collections - Queues

 
Reply to this topicStart new topic

> Introduction to Generic Collections - Queues, Introducing the Queue class

SixOfEleven
Group Icon



post 15 Apr, 2009 - 06:30 AM
Post #1


Introduction to Queues.

Queues are an interesting collection. Queues represent a first-in-first-out(FIFO) collection. Think of a queue like a line at a grocery store. Customers come to the check out and are processed in the order that they came in. If you needed to program that sort of situation you could use a queue.

Queues are used a lot in real life simulations. For example, you need to model how a bank processes customers to find out the best number of tellers to have at the windows at a given time. You would create a simulation that would process how many customers come into the bank, the average wait time of a custmoer. How many customers each teller processes.

You could also use a queue in a client-server application. As each request comes into the server it is put into a queue to be processed. The server would process the requests in the order that they came in.

This is how you would create a queue:

CODE

Dim MyQueue As Queue(Of Class)


Before you can use your queue you need to create it. You do so like this:

CODE

MyQueue = New Queue(Of Class)


When you need to add an object to the queue you use the Enqueue method like this:

CODE

MyQueue.Enqueue(object)


To get the first object in the queue you use the Dequeue method.

CODE

objectVariable = MyQueue.Dequeue()


Sometimes you may want to see what the first object in the queue is without removing it from the queue. You would do this using the Peek method like this:

CODE

objectVariable = MyQueue.Peek()


To determine how many objects are in a queue you would use the Count property like this:

CODE

intNumber = MyQueue.Count


If you need to empty the queue you would use the Clear method like this:

CODE

MyQueue.Clear()


If you need to see the objects in your queue there two easy ways to do it. You can use a simple for loop or a foreach loop. This is how to do each one in turn:

CODE

For I As Integer = 0 To MyQueue.Count - 1
    Console.WriteLine(MyQueue(I).ToString())
Next


CODE

For Each o As Class In MyQueue
    Console.WriteLine(o.ToString())
Next


This is a simple program that demonstrates the use of queues:

CODE

Module Module1
    Private MyQueue As Queue(Of String)

    Sub Main()
        MyQueue = New Queue(Of String)

        MyQueue.Enqueue("Bobby")
        MyQueue.Enqueue("Margaret")
        MyQueue.Enqueue("Timmy")
        MyQueue.Enqueue("Wendy")
        MyQueue.Enqueue("Linda")
        Console.WriteLine("This is the Queue:")

        For I As Integer = 0 To MyQueue.Count - 1
            Console.WriteLine(MyQueue(I))
        Next

        Console.WriteLine()

        Console.WriteLine("Press enter to continue")
        Console.ReadLine()

        While (MyQueue.Count > 0)

            Dim Name As String

            Name = MyQueue.Peek()

            Console.WriteLine("Removing {0} from the Queue", Name)

            MyQueue.Dequeue()

            If MyQueue.Count > 0 Then
                Console.WriteLine()
                Console.WriteLine("This is the Queue:")
            Else
                Console.WriteLine()
                Console.WriteLine("The Queue is empty")
            End If
            WriteQueue()
        End While

        Console.ReadLine()

    End Sub

    Public Sub WriteQueue()

        For Each Name As String In MyQueue
            Console.WriteLine(Name)
        Next
        Console.WriteLine()
    End Sub
End Module

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:22PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month