Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class IndependentQueue(Of T)
- 'Shared variables which are modified by reference
- Private Ends As Object()
- 'Instance variables for each node
- Private ReadOnly Item As T = Nothing
- Private [Next] As IndependentQueue(Of T) = Nothing
- 'Default constructor
- Public Sub New()
- Ends = {Nothing, Nothing, 0}
- End Sub
- 'Internal use constructor
- Private Sub New(ByVal Ends As Object(), ByVal Item As T)
- Me.Ends = Ends
- Me.Item = Item
- End Sub
- 'Returns the number of items in the queue
- Public ReadOnly Property Count() As Integer
- Get
- Return Ends(2)
- End Get
- End Property
- 'Iterates through the queue in search of a specified item
- Public Function Contains(ByVal Item As T) As Boolean
- Dim Head As IndependentQueue(Of T) = Ends(0)
- While Head IsNot Nothing
- If Head.Item.Equals(Item) Then Return True
- Head = Head.Next
- End While
- End Function
- 'Returns whether the queue is empty
- Public ReadOnly Property IsEmpty() As Boolean
- Get
- Return Ends(2) = 0
- End Get
- End Property
- 'Adds an item to the queue
- Public Sub Enqueue(ByVal Item As T)
- If Ends(0) Is Nothing Then
- Ends(0) = New IndependentQueue(Of T)(Ends, Item)
- Ends(1) = Ends(0)
- Else
- DirectCast(Ends(1), IndependentQueue(Of T)).Next = New IndependentQueue(Of T)(Ends, Item)
- Ends(1) = DirectCast(Ends(1), IndependentQueue(Of T)).Next
- End If
- Ends(2) += 1
- End Sub
- 'Removes and returns the next item in the queue
- Public Function Dequeue() As T
- If Ends(0) Is Nothing Then Throw New Exception("Queue is empty")
- Dequeue = DirectCast(Ends(0), IndependentQueue(Of T)).Item
- Ends(0) = DirectCast(Ends(0), IndependentQueue(Of T)).Next
- If Ends(0) IsNot Nothing Then [Next] = DirectCast(Ends(0), IndependentQueue(Of T)).Next
- Ends(2) = DirectCast(Ends(2), Integer) - 1
- End Function
- 'Retrieves the next item without removing it from the queue
- Public Function Peek() As T
- If Ends(0) Is Nothing Then Throw New Exception("Queue is empty")
- Return DirectCast(Ends(0), IndependentQueue(Of T)).Item
- End Function
- 'Clears all items from the queue by dereferencing the head
- Public Sub Clear()
- Ends(0) = Nothing
- Ends(1) = Nothing
- Ends(2) = 0
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement