/// <summary> /// 在节点尾部添加一个节点 /// </summary> /// <param name="value"></param> public void Add(T value) { KillerLinkListNode <T> node = new KillerLinkListNode <T>(value); if (this._head == null) { this._head = node; this._lastNode = node; } else { this._lastNode.NextNode = node; this._lastNode = node; } this.NodeCount++; }
/// <summary> /// 在任意位置插入 /// </summary> /// <param name="index"></param> /// <param name="value"></param> public void Insert(long index, T value) { this._nodeCount++; var newNode = new KillerLinkListNode <T>(value); if (index == 0) { newNode.NextNode = this._head; this._head = newNode; } else { var node = this.GetLinlistNodeByIndex(index - 1); newNode.NextNode = node.NextNode; node.NextNode = newNode; } }
/// <summary> /// 移除链表中任意一个元素 /// </summary> /// <param name="index">所在位置</param> public void RemoveAt(long index) { if (index < 1 || index > this._nodeCount) { throw new IndexOutOfRangeException("索引不在链表的范围内"); } if (index == this._nodeCount) { if (this._nodeCount == 1) { this._lastNode = null; this._head = null; } else { var node1 = GetLinlistNodeByIndex(index - 1); this._lastNode = node1; this._lastNode.NextNode = null; } } else { if (index == 1) { var mid = this._head; this._head = this._head.NextNode; mid.NextNode = null; } else { var node = GetLinlistNodeByIndex(index - 1); var next = node.NextNode; node.NextNode = next.NextNode; next = null; } } this.NodeCount--; }
public void Dispose() { this._head = null; this._lastNode = null; this._nodeCount = 0; }