Advertisement
TizzyT

IndependentQueue -TizzyT

Feb 23rd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.50 KB | None | 0 0
  1. Public Class IndependentQueue(Of T)
  2.     'Shared variables which are modified by reference
  3.     Private Ends As Object()
  4.  
  5.     'Instance variables for each node
  6.     Private ReadOnly Item As T = Nothing
  7.     Private [Next] As IndependentQueue(Of T) = Nothing
  8.  
  9.     'Default constructor
  10.     Public Sub New()
  11.         Ends = {Nothing, Nothing, 0}
  12.     End Sub
  13.  
  14.     'Internal use constructor
  15.     Private Sub New(ByVal Ends As Object(), ByVal Item As T)
  16.         Me.Ends = Ends
  17.         Me.Item = Item
  18.     End Sub
  19.  
  20.     'Returns the number of items in the queue
  21.     Public ReadOnly Property Count() As Integer
  22.         Get
  23.             Return Ends(2)
  24.         End Get
  25.     End Property
  26.  
  27.     'Iterates through the queue in search of a specified item
  28.     Public Function Contains(ByVal Item As T) As Boolean
  29.         Dim Head As IndependentQueue(Of T) = Ends(0)
  30.         While Head IsNot Nothing
  31.             If Head.Item.Equals(Item) Then Return True
  32.             Head = Head.Next
  33.         End While
  34.     End Function
  35.  
  36.     'Returns whether the queue is empty
  37.     Public ReadOnly Property IsEmpty() As Boolean
  38.         Get
  39.             Return Ends(2) = 0
  40.         End Get
  41.     End Property
  42.  
  43.     'Adds an item to the queue
  44.     Public Sub Enqueue(ByVal Item As T)
  45.         If Ends(0) Is Nothing Then
  46.             Ends(0) = New IndependentQueue(Of T)(Ends, Item)
  47.             Ends(1) = Ends(0)
  48.         Else
  49.             DirectCast(Ends(1), IndependentQueue(Of T)).Next = New IndependentQueue(Of T)(Ends, Item)
  50.             Ends(1) = DirectCast(Ends(1), IndependentQueue(Of T)).Next
  51.         End If
  52.         Ends(2) += 1
  53.     End Sub
  54.  
  55.     'Removes and returns the next item in the queue
  56.     Public Function Dequeue() As T
  57.         If Ends(0) Is Nothing Then Throw New Exception("Queue is empty")
  58.         Dequeue = DirectCast(Ends(0), IndependentQueue(Of T)).Item
  59.         Ends(0) = DirectCast(Ends(0), IndependentQueue(Of T)).Next
  60.         If Ends(0) IsNot Nothing Then [Next] = DirectCast(Ends(0), IndependentQueue(Of T)).Next
  61.         Ends(2) = DirectCast(Ends(2), Integer) - 1
  62.     End Function
  63.  
  64.     'Retrieves the next item without removing it from the queue
  65.     Public Function Peek() As T
  66.         If Ends(0) Is Nothing Then Throw New Exception("Queue is empty")
  67.         Return DirectCast(Ends(0), IndependentQueue(Of T)).Item
  68.     End Function
  69.  
  70.     'Clears all items from the queue by dereferencing the head
  71.     Public Sub Clear()
  72.         Ends(0) = Nothing
  73.         Ends(1) = Nothing
  74.         Ends(2) = 0
  75.     End Sub
  76. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement