我已经创建了长链类,每个链接(类)只知道下一个和上一个链接.当索引不重要时,我认为它优于Arrays.
public class ChainLink
{
public ChainLink previousLink, nextLink;
// Accessors here
}
Run Code Online (Sandbox Code Playgroud)
问题:
分配给链:
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)
| 归档时间: |
|
| 查看次数: |
1201 次 |
| 最近记录: |