les stacks

POO ou Classe personnel

les stacks

Messagepar oguruma » 30 Jan 2005 à 12:52

merci à son auteur

Code : Tout sélectionner

Public Class Stack
   
   a As Variant
   top As Integer
   growth As Integer
   
   Public Sub new()
      ' Creates a new empty Stack.
      Redim a(10)
      top = -1
      growth = 2
   End Sub
   
   Public Function push(element As Variant) As Variant
      ' Adds an element on top of the stack and returns it.
      If top > Ubound(a) Then Redim a(Ubound(a) * growth)
      top = top + 1
      If Isobject(element) Then
         Set Me.a(top) = element
         Set Me.push = element
      Else
         Me.a(top) = element
         Me.push = element
      End If
   End Function
   
   Public Function pop() As Variant
      ' Returns the element on top of the stack, and removes it from the stack.
      ' Returns an error if no elements is in the stack.
      Set Me.pop = Nothing
      If top = -1 Then Exit Function
      Dim tmp As Variant
      tmp = Me.peek()
      Set Me.a(top) = Nothing
      top = top - 1
      If Isobject(tmp) Then
         Set Me.pop = tmp
      Else
         Me.pop = tmp
      End If
   End Function
   
   Public Function peek() As Variant
      ' Returns the element on top of the stack without removing it.
      ' Returns an error if no elements is in the stack.
      Set Me.peek = Nothing
      If top = -1 Then Exit Function
      If Isobject(Me.a(top)) Then
         Set Me.peek = Me.a(top)
      Else
         Me.peek = Me.a(top)
      End If
   End Function
   
   Public Function isEmpty() As Variant
      ' Returns true if the stack has no elements, false if not.
      Me.isEmpty = (top = -1)
   End Function
   
   Public Function toArray() As Variant
      ' Returns an array representation of this Stack
      Set Me.toArray = Nothing
      If Me.top = -1 Then Exit Function
      Dim tmp As Variant
      Redim tmp(Me.top)
      Dim i As Integer
      For i = 0 To Me.top
         If Isobject(Me.a(i)) Then
            Set tmp(i) = a(i)
         Else
            tmp(i) = a(i)
         End If
      Next i
      Me.toArray = tmp
   End Function
   
   Public Function toString() As String
      ' Returns a String representation of this Stack.
      Dim tmp As String
      tmp = ""
      Dim i As Integer
      For i = 0 To Me.top-1
         If Isobject(a(i)) Then
            tmp = tmp & a(i).toString()
         Else
            tmp = tmp & a(i)
         End If
         tmp = tmp & ", "
      Next
      If Isobject(a(top)) Then
         tmp = tmp & a(top).toString()
      Else
         tmp = tmp & a(top)
      End If
      toString = tmp
   End Function
End Class

Bien à vous

http://www.dominoarea.org/oguruma/

Les téléphones PORTABLES dans les TGV y en a MARRRE de ces voyageurs qui ne respectent pas les autres ! ARRET DES PORTABLES SVP - Merci

Fumeurs ! respectez les non fumeurs !!!
Fumeurs ! respectez la loi de février 2007 et les lieux publics !!! (ie. hall de gares)
Avatar de l’utilisateur
oguruma
Super V.I.P.
Super V.I.P.
 
Message(s) : 4086
Inscrit(e) le : 16 Déc 2004 à 08:50
Localisation : LILLE

Retour vers Programmation orienté objet