none
VBA Linked List RRS feed

Answers

All replies

  • See https://riptutorial.com/vba/example/26941/linked-list

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, February 6, 2019 8:22 PM
  • Thanks for the code. I created two class modules based on the code. I am not sure how to use this though.

    in a standard module I added the following code, but this doesn't work:

    Sub addNode()
    Dim node As SinglyLinkedNode
           
    Set node = New SinglyLinkedNode
    node.Add (5)

    End Sub

    can you explain how to call this add method?

    Thanks

    Wednesday, February 6, 2019 9:28 PM
  • 1) The SinglyLinkedNode class currently doesn't work. You should either add Let and Get methods, or make both properties (variables) Public:

    Public value As Variant
    Public NextNode As SinglyLinkedNode

    2) Here is an example of how to use the LinkedList class:

    Sub Example()
        Dim lst As LinkedList
        Debug.Print "Create new linked list"
        Set lst = New LinkedList
        Debug.Print "Add values 5 and 12 to the list"
        lst.Add 5
        lst.Add 12
        Debug.Print "The list now has " & lst.Count & " items"
        Debug.Print "Does the list contain 5? " & lst.Exists(5)
        Debug.Print "Does the list contain 12? " & lst.Exists(12)
        Debug.Print "Remove 5 from the list"
        lst.Remove 5
        Debug.Print "The list now has " & lst.Count & " items"
        Debug.Print "Does the list contain 5? " & lst.Exists(5)
        Debug.Print "Does the list contain 12? " & lst.Exists(12)
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, February 6, 2019 10:30 PM
  • Thanks for replying. Yes I made the properties of the SinglyLinkedNode public and was able to access them.

    I am using the LinkedList class to add a node(s).

    i'm still not sure how to set the nextnode property.

    if I create a node using the linkedlist class, how do I then use the node object to point to the next node.

    for example, in your example above how do I now associate the two nodes you created with values 5 and 12 with a node object?

    Wednesday, February 6, 2019 11:29 PM
  • The code from the link I posted is incomplete; I have uploaded a slightly expanded demo to DropBox:

    https://www.dropbox.com/s/ddfdcb4k18rfku8/LinkedLists.xlsm?dl=1


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, February 7, 2019 10:09 AM
  • This is excellent. Just what I need. Thanks!
    • Marked as answer by James N San Friday, February 8, 2019 4:05 AM
    Friday, February 8, 2019 4:04 AM