[Year 12 SofDev] Queues in VB.net
BUCKNELL, Chris
cbucknell at whitefriars.vic.edu.au
Thu Aug 4 10:14:44 EST 2011
Hi Trav,
Here's a little queue example that I've used
Regards Chris
Module Module1
Sub Main()
Dim getYourBurger As New HomeMadeQueue
'Add some people to the queue
Console.WriteLine("Add 'Chris' to the Queue")
getYourBurger.EnQueue("Chris")
Console.WriteLine("Add 'Jill' to the Queue")
getYourBurger.EnQueue("Jill")
Console.WriteLine("Add 'Ted' to the Queue")
getYourBurger.EnQueue("Ted")
Console.WriteLine("Add 'Mel' to the Queue")
getYourBurger.EnQueue("Mel")
'How long is that queue?
Dim numberPeopleInQueue As Integer = getYourBurger.Count
Console.WriteLine("Queue length is:" & numberPeopleInQueue)
'Who is first in line?
Console.WriteLine("The first in line is:" & getYourBurger.Peek)
'Process the queue ... do you want fries with that???
For counter As Integer = 1 To numberPeopleInQueue
Console.WriteLine("Hi " & getYourBurger.DeQueue() & ", what burger would you like?")
Next
'What happens if we try to process an empty queue
Console.WriteLine("Is there anybody out there??? " & getYourBurger.DeQueue())
Console.ReadLine()
End Sub
''' <summary>
''' The following Queue class implementation includes methods for EnQueue,
''' DeQueue, ClearQueue (clearing the queue), Peek, and Count, as well as a
''' default constructor for the class.
'''
''' The underlying data structure used to hold the queue data is an ArrayList. This is
''' used because their dynamic nature, i.e. Insert is added to the next free element of the list,
''' remove from front automatically moves each item in the list up one element, and we don't need
''' to maintain any placeholders (first item, last item etc...)
'''
''' </summary>
''' <remarks>This is closely based on content from "Data Structures and Algorithms Using Visual Basic.NET"
''' by Michael McMillan (ISBN:9780521547659)
''' </remarks>
Public Class HomeMadeQueue
Private _queue As New ArrayList()
Public Sub New()
MyBase.New()
End Sub
Public Sub EnQueue(ByVal item As Object)
_queue.Add(item)
End Sub
Public Function DeQueue() As Object
Dim item As Object
If Count() > 0 Then
item = _queue.Item(0)
_queue.RemoveAt(0)
Else
item = "Queue empty error"
End If
Return item
End Function
Public Function Peek() As Object
Return _queue.Item(0)
End Function
Public Sub ClearQueue()
_queue.Clear()
End Sub
Public Function Count() As Integer
Return _queue.Count()
End Function
End Class
End Module
From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Travis Parker
Sent: Thursday, 4 August 2011 8:16 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: [Year 12 SofDev] Queues in VB.net
Dear All,
I am currently doing some stuff in data structures and going to create a queue example in VB.net. Does anyone have an example of these? I am thinking that I will use a queue as part of U4O1. I've already done a stack example but am going to write a SAC with a waiting list FIFO so need to use a queue.
Any assistance from the brilliant programmers on the list would be much appreciated and save me some time!
Cheers
Trav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20110804/8d0525b6/attachment-0001.html
More information about the sofdev
mailing list