c# - Chain linked classes -
i have created long chains of classes, each link(class) knowing next , previous link. see great advantage on arrays when index not important.
public class chainlink { public chainlink previouslink, nextlink; // accessors here } questions :
- what technique called? (i don't know search)
- is there .net class same thing?
- is there noticeable performance impact vs. array or list?
example of accessors use
assign chain :
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; } shortening chain :
if un-assign next link of chain, leaving remaining links un-referenced main program, garbage collector dispose of data me.
testing circular referencing :
public bool iscircular () { chainlink link = this; while (link != null) { link = link._nextlink; if (link == this) return true; } return false; } offset index :
public chainlink [int offset] { { if (offset > 0 && _nextlink != null) return _nextlink [offset - 1]; if (offset < 0 && _previouslink != null) return _previouslink [offset + 1]; return this; } }
1) structure called double linked list
2) implementation exists in c# through linkedlist
Comments
Post a Comment