Monday, December 26, 2011

Monday 12.26.11

Option Strict On
Imports System
Namespace QueueDemo
Class Tester
Public Sub Run()
Dim intQueue As New Queue()
'populate the array
Dim i As Integer
For i = 0 To 4
intQueue.Enqueue((i * 5))
Next i

'display the queue
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)

'remove an element from the queue
Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue())

'display the Queue
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)

'remove another element from the queue
Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue())

'display the queue
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)

'view the first element in the Queue but do not remove
Console.WriteLine("(Peek) {0}", intQueue.Peek())

'Display the queue
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)
End Sub 'run

Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim myEnumerator As IEnumerator = myCollection.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine("{0} ", myEnumerator.Current)
End While
Console.WriteLine()
End Sub 'DisplayValues

Shared Sub Main()
Dim t As New Tester()
t.Run()
End Sub 'Main
End Class 'Tester
End Namespace 'QueueDemo


Option Strict On
Imports System
Namespace StackDemo
Class Tester
Public Sub Run()
Dim intStack As New Stack()

'populate the stack
Dim i As Integer
For i = 0 To 7
intStack.Push((i * 5))
Next i

'display the stack
Console.WriteLine("intStack values:")
DisplayValues(intStack)
'remove an element from the stack
Console.WriteLine("(Pop){0}", intStack.Pop())

'display the stack
Console.WriteLine("intStack values:")
DisplayValues(intStack)

'remove another element from the stack
Console.WriteLine("(Pop) {0}", intStack.Pop())

'display the Stack
Console.WriteLine("intStack values: ")
DisplayValues(intStack)

'view the first element in the stack
'but do not remove
Console.WriteLine("(Peek) {0}", intStack.Peek())

'display the stack
Console.WriteLine("intStack values:")
DisplayValues(intStack)
End Sub 'Run

Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim o As Object
For Each o In myCollection
Console.WriteLine(o)
Next o
End Sub

Shared Sub Main()
Dim t As New Tester()
t.Run()
End Sub 'Main
End Class ' Tester
End Namespace 'Stack demo


Namespace StackDemo
Class Tester

Public Sub Run()
Dim intStack As New Stack()

'populate the array
Dim i As Integer
For i = 1 To 4
intStack.Push((i * 5))
Next

'display the stack
Console.WriteLine("intStack values:")
DisplayValues(intStack)

Const arraySize As Integer = 10
Dim testArray(arraySize) As Integer

'populate the array
For i = 1 To arraySize - 1
testArray(i) = i * 100
Next i
Console.WriteLine("Contents of the test array")
DisplayValues(testArray)

'copy the intstack into the new array, start offset 3
intStack.CopyTo(testArray, 3)
Console.WriteLine("TestArray after copy:")
DisplayValues(testArray)

'copy the entire source stack to a new standard arry
Dim myArray As Object() = intStack.ToArray()

'display the values of the new standard array
Console.WriteLine("The new array:")
DisplayValues(myArray)
End Sub 'Run

Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim o As Object
For Each o In myCollection
Console.WriteLine(o)
Next
End Sub 'display values

Shared Sub Main()
Dim t As New Tester()
t.Run()
End Sub
End Class
End Namespace

No comments: