Valhalla Legends Forums Archive | Visual Basic Programming | [VB6] Implementing LinkedList structure

AuthorMessageTime
DOOM
Hi. I started work on implementing a LinkedList structure in VB6, but it's giving me crap... I'm more used to doing this sort of thing in Java, so I'm hoping maybe some of you know more about dealing with this sort of thing in Visual Basic.

This is from the LinkedList.cls

[code]
Option Explicit

Private varHead As New LinkedListElement
Private lngSize As Long
Private bIsEmpty As Boolean

Public Sub Init()
Set varHead = Nothing
lngSize = 0
bIsEmpty = True
End Sub

Public Sub add(ByVal value As Variant)
If (varHead Is Nothing) Then
If IsObject(value) Then
Set varHead.setValue = value
Else
varHead.setValue = value
End If
Set varHead.setNextElement = Nothing
Else
Dim insert As LinkedListElement
Set insert = New LinkedListElement
If IsObject(value) Then
Set insert.setValue = value
Else
insert.setValue = value
End If
Set insert.setNextElement = Nothing
Dim temp As New LinkedListElement
Dim bFound As Boolean
bFound = False
Set temp = varHead
Dim newElement As LinkedListElement
Do While (bFound <> True)
Set newElement = temp.getNextElement
If (newElement Is Nothing) Then
Set temp.setNextElement = insert
bFound = True
Else
Set temp = newElement
End If
Loop
End If
lngSize = lngSize + 1
bIsEmpty = False
End Sub

Public Sub remove(ByVal value As Variant)
If (varHead <> Nothing) Then
Dim tempElement As LinkedListElement
Dim tempVal As Variant
Set tempElement = varHead
If (IsObject(tempElement.getValue)) Then
Set tempVal = tempElement.getValue
Else
tempVal = tempElement.getValue
End If
If (value = tempVal) Then
Set tempElement = tempElement.getNextElement
If (tempElement <> Nothing) Then
Set varHead = tempElement
Else
Set varHead = Nothing
bIsEmpty = True
End If
lngSize = lngSize - 1
Else
Dim previous As New LinkedListElement
Set previous = tempElement
Set tempElement = tempElement.getNextElement
While (tempElement <> Nothing)
If (IsObject(tempElement.getValue)) Then
Set tempVal = tempElement.getValue
Else
tempVal = tempElement.getValue
End If
If (value = tempvalue) Then
Dim tempNext As New LinkedListElement
Set tempNext = tempElement.getNextElement
previous.setNextElement = tempNext
lngSize = lngSize - 1
Else
Set previous = tempElement
Set tempElement = tempElement.getNextElement
End If
Loop
End If
End If
End Sub

Public Function isEmpty() As Boolean
isEmpty = bIsEmpty
End Function

Public Function getSize() As Long
getSize = lngSize
End Function

Public Property Get getHead() As LinkedListElement
Set getHead = varHead
End Property
[/code]

And this is from the LinkedListElement.cls:

[code]
Option Explicit

Private value As Variant 'value to be stored in this element
Private varNextElement As LinkedListElement 'next element in the list

'change the next element in the list to a new element
Public Property Set setNextElement(ByVal newNext As LinkedListElement)
Set varNextElement = newNext
End Property

'return the next element in the list
Public Property Get getNextElement() As LinkedListElement
Set getNextElement = varNextElement
End Property

'change the value stored in this element (if it is a primitive type)
Public Property Let setValue(ByVal newValue As Variant)
value = newValue
End Property

'change the value stored in this element (if it is an object type)
Public Property Set setValue(ByVal newValue As Variant)
Set value = newValue
End Property

'return the value stored in this element
Public Property Get getValue()
If (IsObject(value)) Then
Set getValue = value
Else
getValue = value
End If
End Property
[/code]

I was testing it like this:

[code]
Dim ll As New LinkedList
ll.Init

Dim l1 As New LinkedListElement
l1.setValue = "test1"

ll.add l1

Dim l2 As New LinkedListElement
l2.setValue = "test2"

ll.add l2

Dim iterate As LinkedListElement
Set iterate = ll.getHead

lbl.Caption = iterate.getValue
[/code]

No matter what I've done, iterate.getValue doesn't seem to return anything. The isEmpty and getSize functions both return results correctly (ie: telling me that I have added two items to the LinkedList).

Sorry about the long post, but I thought I'd try and give you as much information to work with as I could as opposed to giving too little. I'd really appreciate any help any of you could give. Thanks.
July 5, 2004, 4:36 PM
Grok
[quote]Public Property Get getValue()[/quote]

Try returning a Variant type? "As Variant"
Did you step through Property Get getValue? While in that property, is the value of Value correct?
July 5, 2004, 5:13 PM
DOOM
Hey I think I found the problem. VB wants you to do stuff like this:

dim var as linkedlistelement
set var = new linkedlistelement

It hates me if I try to combine those.
Ie: dim var as new linkedlistelement

So I went through and changed my code in the declarations and add methods and so far it seems to be working. I still need to go through and test the remove method. But so far, so good.

Thanks for the help Grok, it really helped just having someone else looking at it too. I really appeciate the assist.
July 5, 2004, 6:05 PM

Search