Click to See Complete Forum and Search --> : Linked Lists?
Robin Dude
May 10th, 2001, 08:29 PM
I want to create a data-type that has several lists involved in it of unknown length and I don't want to waste memory by having high DIM'd arrays or run out of space with low DIM'd arrays. Additionally, each of these lists within the datatype might have sublists of their own. Thus linked lists pointing to memory locations would seem to fit the bill, but I don't know how to create them in VB.
How do you create linked lists in VB?
Is there a better way to implement this idea? If so, how?
phunkydude
May 11th, 2001, 07:25 AM
Hi Robin,
checkout the Collection Object;
basically it's a list of objects.
In your case you'd want some of those objects, in turn, also as collections.
Another bonus is that you can traverse the list using:
for Object in Collection
...
next Object
It'll definitely be a plus to get to know the collection object.
Hope this helpfull
Cheers
ps. don't forget to rate postings.
coolbiz
May 11th, 2001, 08:13 AM
Reply from 'phunkydude 5/11/01 08:25 am' is also good. If not, you can still use dynamic array to ensure only needed space is allocated. Consider the following:
option Explicit
Enum THSTATUS
UP = 0
DOWN = 1
ALARM = 2
End Enum
Type THINFO
nID as Long
nType as Integer
nStatus as THSTATUS
End Type
dim sTHInfo() as THINFO
dim nMaxTH as Long
' This function MUST BE CALLED at least once before sTHInfo() can be used!!
private Sub InitTHInfo()
nMaxTH = 0
redim sTHInfo(nMaxTH)
End Sub
' Call this function to add new TH
public Function AddTH(nTHID as Long) as Boolean
' trap error
on error then goto err_trap
' allocate new space
redim sTHInfo(nMaxTH+1)
nMaxTH = nMaxTH + 1
' assign new information
sTHInfo(nMaxTH).nID = nTHID
sTHInfo(nMaxTH).nType = 0
sTHInfo(nMaxTH).nStatus = UP
AddTH = true
exit function
err_trap:
' return false and error message
msgbox "error allocating space for TH Info"
AddTH = false
End Function
' Call this function to get the index of the item give the ID
public Function FindTH(nTHID as Long) as Long
dim i as Long
' go through the list
for i = 1 to nMaxTH
if (sTHInfo(i).nID = nTHID) then
' found it - exit out
FindTH = i
exit function
end if
next i
' if we get to here, ID does not exist - return -1
FindTH = -1
End Function
-Cool Bizs
Robin Dude
May 11th, 2001, 09:05 AM
I'm not certain if this will do it, so I'll ask a few questions:
1) Would this allow me to do sub arrrays and sub-sub arrays that are unique to each element of the array?
That is, suppose I have a main grouping A and A can have any number of entries, and within A each item has multiple entries (take B as a single element of A) that are again unspecified in number, and each B has two or three elements of it that are of unknown length as well. Can this method handle all that? Is it possible to have part of a type declaration be an array of another type? Something like:
Type E
nItemCheck1 as String
nItemCheck3 as Long
End Type
Type D
nItemModifier1 as Integer
nItemModifier2 as Byte
nItemModifier3 as Boolean
End Type
Type C
nItemInfo1 as Integer
nItemInfo2 as Integer
nItemInfo3 as Array() as E
nItemInfo4 as Array() as D
End Type
Type B
nItem as Array() as C
End Type
Type A
nItemType as Array() as B
End Type
coolbiz
May 11th, 2001, 03:07 PM
Changes in your code so that it will work:
Type E
nItemCheck1 as string
nItemCheck3 as Long
End Type
Type D
nItemModifier1 as Integer
nItemModifier2 as Byte
nItemModifier3 as Boolean
End Type
Type C
nItemInfo1 as Integer
nItemInfo2 as Integer
nItemInfo3() as E
nItemInfo4() as D
End Type
Type B
nItem() as C
End Type
Type A
nItemType() as B
End Type
dim sItemInfo as A
Sub InitItem()
' this subroutine will reset the array back to 0 item
dim nInd as Long
nInd = 0
' allocate space start from 0 for type B
redim sItemInfo.nItemType(0)
' init the type C
redim sItemInfo.nItemType(0).nItem(0)
' init the type D and E
redim sItemInfo.nItemType(0).nItem(0).nItemInfo3(0)
redim sItemInfo.nItemType(0).nItem(0).nItemInfo4(0)
End Sub
But MAKE SURE TO INITIALIZE each array member to at least 0 index before continuing working with the item. It is shown in InitItem() sub-routine above when item 0 is allocated. The reason is if you DO NOT/FORGET to allocate at least 1 member of the array, any operation on the array prior to that will cause your program to throw an exception RUN-TIME error and crash if not handled properly.
-Cool Bizs
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.