VB.Net/Data Structure/Queue

Материал из VB Эксперт
Версия от 12:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Queue Demo: enqueue, dequeue and peek

Imports System
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
             Dim intQueue As New Queue(  )
             Dim i As Integer
             For i = 0 To 4
                 intQueue.Enqueue((i * 5))
             Next i
             Console.WriteLine("intQueue values:")
             DisplayValues(intQueue)
             Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue(  ))
             Console.WriteLine("intQueue values:")
             DisplayValues(intQueue)
             Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue(  ))
             Console.WriteLine("intQueue values:")
             DisplayValues(intQueue)
             Console.WriteLine("(Peek)   {0}", intQueue.Peek(  ))
             Console.WriteLine("intQueue values:")
             DisplayValues(intQueue)
   End Sub
   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
End Class


Queue Item Count

Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim m_Queue As New Queue
        m_Queue.Enqueue("Text")
        m_Queue.Enqueue("Text")
        m_Queue.Enqueue("Text")
        Dim txt As String = DirectCast(m_Queue.Dequeue(), String)
        Console.WriteLine(txt)
        For Each str As String In m_Queue.ToArray()
            Console.WriteLine(str)
        Next str
        Console.WriteLine(m_Queue.Count )
        
    End Sub
End Class


Simple Demo for Queue: Enqueue, Dequeue and Peek

Imports System
Imports System.Collections
 
Public Class MainClass
    Shared Sub Main(ByVal args As String())
             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
    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
End Class