• I have a project going that is a part excersize to learn the basics of classes, and partly because I would like to use linked lists in another project. I would like create a linked list class from scratch that can create a node as well as perform certain operations to be determined later. I am going by the video tutorials at http://www.youtube.com/watch?v=Ju5q1hhFCso which is a first of 9 tutorials.


    Below shows the class Cnode, and below that is a subroutine located in a module that tests the class. The cNode class I copied from either here, or somewhere on the internet. So far the class seems to work, I think. The only part that does not yet seem to work is I cannot set the next of node q to be Null in the text subroutine below. Does anyone have any idea why it is not working?










  • Re: Linked Lists


    Null is a data type so you can't use it with Object variables - replace Null with Nothing.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Linked Lists


    [FONT=&quot]I had some time to watch the videos again and took the notes below. [/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]my notes are not necessarily correct, some of the notes are actually unanswered questions. For anyone familiar with linked lists, if you see an error or understand something a different way please post it here.[/FONT]


    1. A singly linked list is a data structure, specifically a node data structure in which serves as a foundation for other node based data structures (trees and graphs).
    2. In some circumstances linked lists are more efficient than arrays or array lists.
    3. Unlike many other data structures, a user can create the linked list data structure themselves.
    4. A node only holds two pieces of information, a value (the cargo) and a reference to another node.
    5. A linked list can two things only - a node that refers to linked list, or null. A node that refers to another node is also a node that refers to a linked list since a linked list can be just 1 node that refers to something (points to).
    6. An empty linked list contains no nodes.
    7. Lists lists are drawn using box and pointer diagrams where the box is a node and the pointer is a reference to someplace.
    8. In VBA, use Nothing instead of Null.
    9. If a node is the last node in a linked list, it may have a next that points to either null or to itself.
    10. The next of a node can point to any linked list, including itself. A linked list that has a node that points to another node within the linked list (including itself) will hold an infinite number of data points.
    11. If node A points to node B, then node B exists on a linked list. If on this same list no node points to A, then A does not exist. If A does not exist the what points to B?.
    12. If node A points to node B, and node Z points to node B, you cannot get from Node Z to Node B to Node A.
    13. When creating a linked list, start with the last node since it has a next that is known, then work backwards from that.
    14. A linked list that holds 1 data value has 1 node that points to null. A linked list that holds no data values has no nodes (list is null). A linked list that points to any node within itself holds infinite data values.
    15. A string can be empty but still exist ( ""), similarly with an array(0 to 0). In a linked list, if there exist no nodes then the linked list must be null, does the linked list exist?
    16. Nodes can be counted ie 1 node, 2 nodes, etc, linked lists cannot be counted?
  • Re: Linked Lists


    If you do a binary tree you'll need 2 nodes and a data spot.



    Then you can do binary tree stuff...

  • Re: Linked Lists


    Thanks. A binary tree sounds like something that I would study in the future as I am still working on understanding linked lists. The node class writtne above seems to work ok so far. I might try to bundle it with the drawnode subroutine to see if I can draw a node on the worksheet at the same time a node is created in the class so I can visually confirm how the process is working.

  • Re: Linked Lists


    Typically in a class that will teach linked lists they'll focus on binary trees as well.



    ... and remember usually in a linked list you can not traverse the stack. Ususally it's FIFO type operations ( first in first out ) or it's push/pull ( push onto stack and pull/pop off stack )




    So the class should not have a "NEXT" type operation.

  • Re: Linked Lists


    First In First Out .... like standing in line at the bank. You enter the line at the end and exit the line at the top.



    The Push/Pull is like the stack of plates at your local buffett. You place new plates on top of the pile and remove from the top of the pile.




    http://www.vb-helper.com/tut2.htm#Linked


    here's some light reading



    http://www.developershandbook.com/Downloads/1951c06.pdf



    also


    http://www.brainbell.com/tutors/Visual_Basic/Pointers.htm

  • Re: Linked Lists


    Those sites are were very good sources of infomration. One of them I know I have seen it before a couple a few years ago. I am seeing alot of applications for node data structures. I did not even know about those things until I tried and failed to write a program that would store data in a tree structure. GCExcel created one for me that works well and I want to see if I can do it once I get linked lists understood. I especially liked the one that talks about the pointers and the differences between languages that have a pointer type and VBA which does not.

  • Re: Linked Lists


    I thought i would report back in...I have not had alot of time to move forward with this project but have been reading on classes and watching some videos. Two videos I have been watching to learn some fundamentals are below. Some of the concepts I have been working on
    -What is a constructor and what it is purpose? So far my understanding is a constructor occurs when the object is initialized. For example


    set Obj = new cSomeClass


    In VBA cSomeClass can have a special portion called "Initialize" that will run code when the class is instanciated. The class gets instanciated at the expression = New cSomeClass


    Wikipedia's definition of Constructor .... Constructor = A subroutine that meets 2 conditions 1. It is located within a Class. 2. It creates an Object.


    -Does the object that the constructor creates have to be an object of the class it is located within?
    -Does a constructor have to be run only when the class is initialized for the first time?


    The videos are below, but they are in Java. I have had to watch them a few times but they are staring to make more sense.


    http://www.youtube.com/watch?v=htzJdKoEmO0
    http://www.youtube.com/watch?v=-c4I3gFYe3w

  • Re: Linked Lists


    Eventually yes. The goal for these latest posts from me is not so much the project itself, but for me is having a better understanding classes. If I can complete a project where I make the linked list class myself, then use that class to complete a project involving trees/graphs, then I will have a better understanding of classes. My goal is to use general principals that can apply to most object oriented languages. I figure if the concepts are understood, the details will take care of themselves. The "constructor" seems to be a fundamentally important concept.

  • Re: Linked Lists


    [FONT=&quot]Hello Again Ozgrid,[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]I have not been here for awhile and I unfortunately let this project stall out. I am reading about the subject again and watching videos - I happened to discover today one of the very best video series I have found yet that explains some basic concepts of OOP. The quality of the instruction is beyond anything I have seen on youtube for quiet awhile.[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]https://www.youtube.com/watch?…FEs41ueBVMW0WnumNhNO7xdxg[/FONT]
    [FONT=&quot] [/FONT]
    [FONT=&quot]I will post here if I make any progress and I welcome any helpful comments/tips[/FONT]

  • Re: Linked Lists


    I thought I had better spend some time on classes / linked lists again to try to get something done with that project. One of the points of studying linked lists for me is to try to understand classes a little better.


    In VBA I created a module and in that a sub routine called cTest


    Code
    Option Explicit
    
    
    Sub cTest()
    
    
    End Sub


    I then created a class module and named it cNode


    Code
    Option Explicit


    So I have so far a subroutine that is empty, and a class that has no code. Of course no errors. I change the code to cTest to below



    When I run the code for cTest, it breaks at the Stop as expected. What is unexpected to me, is that it shows that there exists what the locals window calls and Expression called c. It shows the value of c is Nothing and it shows that it has a type of cNode.


    Question - After the expression


    Code
    Dim c as cNode


    was an object created? Does there now exist an object called c with a value of nothing? I have been reading about constructors, mostly in Java but some in VBA. A Class is only a blueprint for an object and when I created the Class cNode in a class module, I was only creating a blueprint for some future object. In order for the object to be created, it is "contstructed" using a constructor. My assumption is that I would have to explicitly state in a subroutine when it is time to create an object from a class. This is explicitly done by


    Code
    Set c = New cNode


    The New keyword in the above means I think the same thing as "Use the cNode blueprints to build and object as specified. When that object has been built, set a pointer from the variable c to that object.


    The updated code is shown below



    This time, the locals window shows an Expression c, but it now has a + sign to the left of it. When expending that plus sign, nothing is shown. There is no longer a value associated directly with c anymore, instead it says "No Variables" where the plus sign opened up. c Still has a type as before, it is of type cNode. I start starting to understand this and the results in the locals window a bit better now. Earlier in this post when the code was as shown below


    Code
    Sub cTest()
    
    
    Dim c As cnode
    
    
    Stop
    
    
    End Sub


    The c that was shown in the locals was not an object at all, it had never been constructed. It was being shown in a similar way as a string would be shown or an integer.

  • Re: Linked Lists


    I am pretty sure that "Nothing" is only used when referring to objects. So with the code below


    Code
    Sub cTest() 
         
        Dim c As cnode 
         
        Stop  
         
    End Sub


    An object must have been created, because the locals window says that c has a value of Nothing, which is a value that I think only applies to objects.


    Q - Does Dim c as cNode create an object? If it does, then what is the point of set c = New cNode? Any comments appreciated

  • Re: Linked Lists


    Currently the code in my Class module is



    The name of the class is cNode


    The code in the module that utilizes the class is below



    When I run this code, it breaks at the Stop as expected. In the locals window, c is shown with the plus to the left. When I open the plus, it then shows both pdata and cdata as expressions and each has a value of "abc" and a type of string.


    Why is pData visible in the locals window? I thought it should be hidden since I used the "Private" keyword when I declared it in the class module?


    When I typed c. intellisence correctly just showed the only possibility to be c.cData as an option, and did not show c.pData as an option, which seems correct.

  • Re: Linked Lists


    Thanks, that's a good article I have read it. One of the reasons I am having some trouble is I am learning from books based on Java, which does not always translate well to VBA I guess because VBA is not a true OOP language. One of the things I would l ike to do in this node class is to be able to pass parameters.




    ..But there does not appear to be an easy way to do this. There are workarounds but I am not understanding how those workarounds are working.



    Instead, I am guessing the way to do it is like so



Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!