Creating dynamic data structures(from flws)

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: 

时间: 2024-10-17 01:10:28

Creating dynamic data structures(from flws)的相关文章

无锁数据结构(Lock-Free Data Structures)

原文:无锁数据结构(Lock-Free Data Structures) 一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Server里的共享数据结构,例如缓存池里的页(通过闩锁(Latches)),锁管理器哈希表里的锁(通过自旋锁(Spinlock)).接下里你会看到越来越多的全新同步原语(synchronization primitives),即所谓的

Creating custom data graphics in Visio

Creating custom data graphics in Visio https://blogs.office.com/2014/06/30/creating-custom-data-graphics-in-visio/

剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

  按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Data Structures>> # Release 3.0 # chengang882 @ 2016-12-20 # 它可以检查常用的语法里,({[]})这些符号是否是正常闭合的 # Completed implementation of a stack

泛函编程(5)-数据结构(Functional Data Structures)

  编程即是编制对数据进行运算的过程.特殊的运算必须用特定的数据结构来支持有效运算.如果没有数据结构的支持,我们就只能为每条数据申明一个内存地址了,然后使用这些地址来操作这些数据,也就是我们熟悉的申明变量再对变量进行读写这个过程了.试想想如果没有数据结构,那我们要申明多少个变量呢.所以说,数据结构是任何编程不可缺少的元素.     泛函编程使用泛函数据结构(Functional Data Structure)来支持泛函程序.泛函数据结构的特点是"不可变特性"(Immutability)

关于dynamic data

问题描述 最近用python写模拟登陆的小程序,涉及到一个cookie的问题,发现post完用户名和密码之后返回的数据里面会有"Dynamicdata",里面有cookie_session,但是这个cookie_session是不会自动带到我程序里面的cookie变量里面的,请问这个Dynamicdata是个什么工作机制? 解决方案 解决方案二:用fiddler调试下,看看到底返回的原始数据是什么.解决方案三:原始数据就是一个json里面对象里面带个cookie_session

Programming Clojure - Unifying Data with Sequences

In Clojure, all these data structures can be accessed through a single abstraction: the sequence (or seq).  A seq (pronounced "seek") is a logical list, the seq is an abstraction that can be used everywhere.   Collections that can be viewed as s

bigtable: A Distributed Storage System for Structured Data

bigtable: A Distributed Storage System for Structured Data http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//archive/bigtable-osdi06.pdf http://www.dbthink.com/?p=493, 中文翻译   总结 A Bigtable is a sparse, distri

C#常用算法:Dynamic Object

Dynamic Object包含在.Net4.0中,该对象可以允许我们在运行时中改变它的类型,有了这种万金油还有什么问题是我们不能解决的呢?我们来看看它的几个使用例子吧: Var类型和Dynamic类型 public static void TestDynamicObject() { //var object and dynamic object var varObj = "this is a var object"; dynamic dyObj = "this is a d

Redis+KVStore: Disk-based Storage for Massive Data

What do we do when data exceeds the capacity but has to be stored on disks? How can we encapsulate KVStore and integrate it into Redis? How is Redis encoding implemented? This article aims to address these questions by using the Ardb protocol, specif