using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 双向循环链表 /// /// public class CircleList { /// /// 头节点 /// public DoubleLinkNode First { get; private set; } /// /// 尾节点 /// public DoubleLinkNode Last { get; private set; } /// /// 链表长度 /// public int Count { get; private set; } /// /// 添加指定节点 /// /// public void AddNode(DoubleLinkNode item) { if (First == null && Last == null) { First = item; Last = item; } First.PreNode = item; Last.NextNode = item; item.PreNode = Last; item.NextNode = First; Last = item; Count++; } /// /// 通过节点数据添加节点 /// /// public void AddNode(T item) { DoubleLinkNode _node = new DoubleLinkNode(item); AddNode(_node); } /// /// 移除指定节点 /// /// public void RemoveNode(DoubleLinkNode node) { node.PreNode.NextNode = node.NextNode; node.NextNode.PreNode = node.PreNode; if (node == First) { First = node.NextNode; } if (node==Last) { Last = node.PreNode; } Count--; } /// /// 清除链表数据 /// public void Clear() { First = null; Last = null; Count = 0; } } /// /// 节点类,用来存储数据 /// /// public class DoubleLinkNode { public DoubleLinkNode(T item) { Value = item; } /// /// 该节点存储的数据 /// public T Value { get; set; } /// /// 上一个节点 /// public DoubleLinkNode PreNode { get; set; } /// /// 下一个节点 /// public DoubleLinkNode NextNode { get; set; } }