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