链链接类

0 c# arrays reference

我已经创建了长链类,每个链接(类)只知道下一个和上一个链接.当索引不重要时,我认为它优于Arrays.

public class ChainLink
{
    public ChainLink previousLink, nextLink;

    // Accessors here
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 这种技术到底叫什么?(我不知道要搜索什么)
  2. 有一个.Net类做同样的事情吗?
  3. 对阵列或列表是否有明显的性能影响?

我使用的访问器示例

分配给链:

    public ChainLink NextLink {
        get{ return _nextLink;}
        set {
            _nextLink = value;
            if (value != null)
                value._previousLink = this;
        }
    }

    public void InsertNext (ChainLink link)
    {
        link.NextLink = _nextLink;
        link.PreviousLink = this;
    }
Run Code Online (Sandbox Code Playgroud)

缩短链条:

如果我取消分配链的下一个链接,让剩余的链接不被主程序引用,垃圾收集器将为我处理数据.

测试循环引用:

    public bool IsCircular ()
    {
        ChainLink link = this;
        while (link != null) {
            link = link._nextLink;
            if (link == this)
                return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

抵消指数:

    public ChainLink this [int offset] {
        get {
            if (offset > 0 && _nextLink != null)
                return _nextLink [offset - 1];
            if (offset < 0 && _previousLink != null)
                return _previousLink [offset + 1];
            return this;
        }
    }
Run Code Online (Sandbox Code Playgroud)

PMe*_*let 5

1)该结构称为双链表

2)此实现存在于C#到LinkedList中

3)关于这个主题有很多文章: 这里这个SO帖子