/// <summary> /// 移动 /// </summary> /// <param name="step"></param> public void Move(int step) { if (step < 1) { throw new ArgumentOutOfRangeException("step", "步数不能小于1"); } for (int i = 1; i < step; i++) { currPrev = currPrev.Next; } }
/// <summary> /// 根据索引获取节点 /// </summary> /// <param name="index"></param> /// <returns></returns> private CirNode <T> GetNodeByIndex(int index) { if (index < 0 || index > count) { throw new ArgumentOutOfRangeException("index", "索引超出范围"); } CirNode <T> cirNode = tail; for (int i = 0; i <= index; i++) { cirNode = cirNode.Next; } return(cirNode); }
/// <summary> /// 获取所有节点信息 /// </summary> /// <returns></returns> public string GetAllNodes() { if (count == 0) { throw new NullReferenceException("该链表中无数据"); } else { CirNode <T> head = tail.Next; string result = ""; for (int i = 0; i < count; i++) { //result += GetNodeByIndex(i).Item + " "; result += head.Item + " "; head = head.Next; } return(result); } }
/// <summary> /// 移除当前节点 /// </summary> public void Remove() { if (tail == null) { throw new NullReferenceException("链表中没有元素"); } else if (count == 1) { tail = null; currPrev = null; } else { if (currPrev.Next == tail) { tail = currPrev; } //移除当前节点 currPrev.Next = currPrev.Next.Next; } count--; }
/// <summary> /// 添加节点 /// </summary> /// <param name="value"></param> public void Add(T value) { CirNode <T> cirNode = new CirNode <T>(value); if (tail == null) { tail = cirNode; tail.Next = tail; currPrev = cirNode; } else { cirNode.Next = tail.Next; tail.Next = cirNode; if (currPrev == tail) { currPrev = cirNode; } tail = cirNode; } count++; }
public MyCircularLinkedList() { count = 0; tail = null; }