首页 > 开发 > 综合 > 正文

Creating dynamic data structures(from flws)

2024-07-21 02:22:06
字体:
来源:转载
供稿:网友
creating dynamic data structures.
in the first part of this series - creating custom collections - i showed how to wrap a .net collection class to create a new enumerable collection with a custom name. in that article i used an arraylist as my underlying storage container. was that a good choice? should i have used a hashtable? what's the difference anyway?
well, as you are probably aware, there are many differences between a hashtable and an arraylist, but what you may not already know is that these differences can dictate how the underlying data is stored in memory. in this article i'll explain the different structures that are used to store data in memory and demonstrate why different collection classes require different underlying structures.
what are collections
collections are structures that allow programs to store and retrieve various items of data in a structured manner. the standard .net collections include:
·     hashtable
·     arraylist
·     stack
·     queue
an important feature of collections is that the total number of items that they contain is (generally) unknown until runtime, i.e.: each time an item is added to a collection the actual structure that houses the data is altered.
dynamic vs. static data storage
many data structures - such as int32 and arrays - are static. this means that their structure is completely defined at design time. while the field values of these structures can be altered by the program, the structure itself cannot.
dynamic data structures allow their structure to by altered by the actual program, and because of this, they are ideal candidates to act as the underlying storage container for the items of a collection. a dynamic data structure that underlies a collection can expand or contract as items are added or removed from the list.
two examples of common dynamic data structures are linked lists and binary trees.
linked list
without going into any great depth, a linked list is a list of items; each item is, itself a structure, and contains a value and a pointer (or reference) to the next item in the list. adding a new item to a linked list simply means creating a new instance of an item and adding a reference to it from the item that is currently at the tail of the list.
the following snippet of psuedo-code shows how you might create a linked list of fish, and then graphically displays what that structure would look like:
class myfishes
    public property headfish as fish
    public property tailfish as fish
    
    public method addfish( newfish as fish )
        
        if isnothing( me.headfish ) then
            me.headfish = newfish
            me.tailfish = newfish
        else
            me.tailfish.nextfish = newfish
            me.tailfish = newfish
        end if
    end method
end class

class fish
    public property name as string
    public property nextfish as fish
end class

dim thelistoffishes as new myfishes()

thelistoffishes.addfish( new fish( "fred" ) )
thelistoffishes.addfish( new fish( "martin" ) )
thelistoffishes.addfish( new fish( "rob" ) )
thelistoffishes.addfish( new fish( "karen" ) )
        
        +--------------------+
        |  thelistoffishes   |
        +--------------------+
                |
    +-----------------------------------+
    |                                   |
+--------+  +--------+  +--------+  +--------+
|        |  |        |  |        |  |        |
|  fred  |->| martin |->|  rob   |->|  karen |-> nothing
|        |  |        |  |        |  |        |
+--------+  +--------+  +--------+  +--------+

note: the last child in a linked list contains a pointer with
      a null reference
you use a linked list when it is important to retrieve items in a manner that is relevant to the order in which they were added to the list. take the following psuedo-code snippet which shows how you might normally retrieve these records from this type of structure: 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表