Re: Linked Lists
I had some time to work on the Linked List project. Code below. I was stuck on the concept of encapsulation for quiet awhile. I would make the sList class and then try to pass an sListNode Object to one of the sList's methods as a parameter. That seemed to defeat the purpose of encapsulation, finally I figured out that I did not need to pass an sListNode Object as a parameter, but only the "cargo" of that obect and just let the sList class handling creating the sListNode Objects as needed. I still have more methods to write for this class but I think I should not have to much trouble from this point.
The sList class. All the methods that work with sListNode Objects are in this class. 3 have been written so far.
Option Explicit
'The sList class is created to address at least 2 scenarios with attempting methods directly in the sListNode class
'1. If two variables, x and y point to the same SListNode (linked list), then operations will change sListNode for one variable but not the other
'Set x = New sListNode("Soap",x) ==> A new sListNode is created. The item is set to "Soap". The Next is set to point to the old value of x. Finally x is assigned to this n ew list
'x and y are now two different linked lists
'2. How would you represent an empty list? If an empty list is represented by Nothing (null in Java), you will get a run time error by calling a sListNode method on x.
'The solution to these two problems in Java (and maybe VBA) is a separate sList class (as opposed to the sListNode class). The job of the sList class is to maintain the
'head of the list in a separate data structure. The Head is similar to a sListNode, except that instead of the item (field?), it has a Size field. Instead of a Next field
'it has a field called Head.
'When initialized by for example set q=New sList, pSize will be 0 and pHead will be Nothing. The New keyword causes an object to be created from sList. As drawn
'that object is simply drawn schematically as square or rectangle to represent a "blank" object. Private pSize and Private pHead below create a property each of that object. After
'Those two statements, there now an object that is no longer "blank", it actually now has two properties by default pSize is set to 0 and pHead is set to Nothing.
'These properties are created whenever the sList is initialized or created for the first time. The methods below (either Sub subname() or Function FunctionName() are
'Not called when the object is initialized for the first time. They are only called explicitly, sListObject.SomeMethod
'This sList object is NOT part of the linked list
'The head property of an sList always points to a Linked List, even for an empty linked list. Null or Nothing is an empty linked list.
Private pSize As Long
Private pHead As sListNode
'This sList class encapsulates the sListNode class and prevents the user
'from being able to directly access its properties or methods
'as well as solves the two problems mentioned in #1 and #2 above.
'Method sInsertFront will insert an sListNode object at the front of the linked list that pHead points to
'These methods should be sketched out on paper. Note that for any object, it must be pointed to in order to "exist". If during the process
'of rearranging pointers, a linked list even momentarily is not "pointed to", it will be lost and garbage collected. So the order
'in which pointers are rearranged is important
'Note that sInsertFront does not take in an sListNode as a variable, although it could. An sList Object is designed to encapsulate an sListNode Object
'the user should not need or be able to to directly work with sListNode's properties or methods in their code.
'Only the "cargo" of an sListNode is sent as a parameter. An sList Object will take care of creating all sListNode Object(s) and
'putting the parameter "cargo" in the item property of an sListNode Object. For now, it is assumed that the pData property (Cargo) of an sListNode Object
'is always a string. Later it may be possible to chagne it to type Variant or Object.
Sub sInsertFront(ByRef sListNodeCargo As String)
'It is possible to have the parameter to an sListNode object, but that defeats the purpose of encapsulation, I think.
'The user should not be directly working with an sListNode's properties or methods. Once these Classes are finished, the
'user should not need to work directly with Nodes at all.
Dim aNewNode As sListNode
Set aNewNode = New sListNode
aNewNode.sSetData = sListNodeCargo
If pSize = 0 Then
Set pHead = aNewNode
Else
Set aNewNode.sSetNext = pHead
Set pHead = aNewNode
End If
pSize = pSize + 1
End Sub
Function sGetLastNode() As sListNode
'Gets the last sListNode Object in the linked list pointed to by this instance of the sList Object's pHead property
'Noted that me.pHead does not seem to work, not sure why. Using pHead instead of me.pHead seems to work.
Set sGetLastNode = pHead
Do While Not sGetLastNode.sGetNext Is Nothing
Set sGetLastNode = sGetLastNode.sGetNext
Loop
End Function
Function sLength() As Long
'Returns the number of sListNode objects that are in the linked list that this instance of the sList Object's pHead property points to
sLength = pSize
End Function
Display More
The sListNode Class
Option Explicit
Private pdata As String
Private pnext As sListNode
Property Let sSetData(value As String)
pdata = value
End Property
Property Get sGetData() As String
sGetData = pdata
End Property
Property Set sSetNext(value As sListNode)
Set pnext = value
End Property
Property Get sGetNext() As sListNode
Set sGetNext = pnext
End Property
Display More
The code below just tests some of the methods of the sList class
Sub test3333()
Dim i As Long
Dim j As Long
Dim h As sListNode
Dim q As sList
Dim String1 As String
Set q = New sList
For i = 1 To 4
q.sInsertFront (CStr(i))
Next i
Set h = q.sGetLastNode
Stop
End Sub
Display More