June 18, 2008

Continued: Creating a Queue data type in QTP

DONG, Housekeeping message: If you have questions, additional information, an opinion or just something to share, you are very welcome to leave it in the comments. I like to get personal mail too, but as mentioned on coding horrors: on a blog, the comments are the best part. And since I just started, I still need some!

In addition to a question I received through email: The class to create a queue on the linked list I published Monday, and how to perform a count on this queue.

Public function queue()
  set queue = new clsQueue
End Function

Class clsQueue

  Private ll

  Private Sub Class_Initialize  ' Setup Initialize event.
    set ll = linkedList     ' Create a linked list instance
    End Sub

  Public function pop()
    If ll.count = 0 Then exit function  ' No items in the list
        pop = ll.getfirst()     ' Return the last item
    ll.deleteFirst        ' and delete it
  End Function

  Public sub push(element)    
    ll.add element        ' Add an item to the list
    End sub

  Public function peek()      
    peek = ll.getfirst()      ' Peek at the top item
  End Function

  Public property get count()
    'return the amount of nodes
    count = ll.count
  End Property
  
End Class

And this is the part where inheritance and polymorphism is missing. The class looks kind of the same as the stack class, but .getlast() is replaced by .getfirst(). I wish everything in life was this simple… The count property is added, so you can now count the items in the queue. If you want to add a count property to the stack, just do the same in the clsStack.

1 comment:

Stefan Thelenius said...

Keep up the good work!

Regards

/Stefan