Page 1 sur 1

Les queues

MessagePublié: 30 Jan 2005 à 12:51
par oguruma
Merci à son auteur

Code : Tout sélectionner
Private Class QueueNode
   item As Variant
   link As QueueNode
   Public Sub new(inItem As Variant, inLink As QueueNode)
      If Isobject(inItem) Then
         Set Me.item = inItem
      Else
         Me.item = inItem
      End If
      Set Me.link = inLink
   End Sub
   Public Function getItem() As Variant
      If Isobject(Me.item) Then
         Set Me.getItem = Me.item
      Else
         Me.getItem = Me.item
      End If
   End Function
   Public Function getLink() As QueueNode
      Set Me.getLink = Me.link
   End Function
   Public Sub setLink(inLink As QueueNode)
      Set Me.link = inLink
   End Sub
End Class

Public Class Queue
   front As QueueNode
   rear As QueueNode
   items As Integer
   
   Public Sub new()
      ' Creates a new Queue
      items = 0
   End Sub
   
   Public Function isEmpty() As Variant
      ' Returns true if the queue has no elements, false if not
      Me.isEmpty = (items = 0)
   End Function
   
   Public Sub insert(element As Variant)
      ' Puts the specified element in the last position in the queue
      If Isobject(element) Then If element Is Nothing _
      Then Error 2000, "The value [Nothing] can not be inserted into queues"
      Dim tmp As New QueueNode(element, Nothing)
      If rear Is Nothing Then
         Set front = tmp
         Set rear = tmp
      Else
         Call rear.setLink(tmp)
         Set rear = tmp
      End If
      items = items + 1
   End Sub
   
   Public Function remove() As Variant
      ' Returns the first element in the queue and removes it from the queue
      Print "Entered remove()"
      Set Me.remove = Nothing
      If items = 0 Then Exit Function
      Dim tmp As Variant
      Print "getting front item..."
      If Isobject(Me.front.getItem()) Then
         Set tmp = Me.front.getItem()
      Else
         tmp = Me.front.getItem()
      End If
      Print "getting front..."
      Set front = front.getLink()
      If front Is Nothing Then Set rear = Nothing
      items = items - 1
      If Isobject(tmp) Then
         Set Me.remove = tmp
      Else
         Me.remove = tmp
      End If
   End Function
   
   Public Function getFront() As Variant
      ' Returns the front of the queue, without removing it from the queue
      If Isobject(Me.front.getItem()) Then
         Set Me.getFront = Me.front.getItem()
      Else
         Me.getFront = Me.front.getItem()
      End If
   End Function
   
   Public Function toArray() As Variant
      ' Returns an array representation of this queue
      Dim a As Variant
      Redim a(items-1)
      Dim node As QueueNode
      Set node = front
      Dim i As Integer
      For i = 0 To items - 1
         If Isobject(node.getItem()) Then
            Set a(i) = node.getItem()
         Else
            a(i) = node.getItem()
         End If
         Set node = node.getLink()
      Next i
      Me.toArray = a
   End Function
   
   Public Function toString() As String
      ' Returns a String representation of this Queue
      Dim i As Integer
      Dim node As QueueNode
      Set node = front
      Dim tmp As String
      If items > 2 Then
         For i = 0 To items - 2
            If Isobject(node.getItem()) Then
               tmp = tmp & node.getItem().toString()
            Else
               tmp = tmp & node.getItem()
            End If
            tmp = tmp & ", "
            Set node = node.getLink()
         Next i
      End If
      If Isobject(node.getItem()) Then
         tmp = tmp & node.getItem().toString()
      Else
         tmp = tmp & node.getItem()
      End If
      toString = tmp   
   End Function
End Class