CircleList.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 双向循环链表
  6. /// </summary>
  7. /// <typeparam name="T"></typeparam>
  8. public class CircleList<T>
  9. {
  10. /// <summary>
  11. /// 头节点
  12. /// </summary>
  13. public DoubleLinkNode<T> First
  14. {
  15. get;
  16. private set;
  17. }
  18. /// <summary>
  19. /// 尾节点
  20. /// </summary>
  21. public DoubleLinkNode<T> Last
  22. {
  23. get;
  24. private set;
  25. }
  26. /// <summary>
  27. /// 链表长度
  28. /// </summary>
  29. public int Count
  30. {
  31. get;
  32. private set;
  33. }
  34. /// <summary>
  35. /// 添加指定节点
  36. /// </summary>
  37. /// <param name="item"></param>
  38. public void AddNode(DoubleLinkNode<T> item)
  39. {
  40. if (First == null && Last == null)
  41. {
  42. First = item;
  43. Last = item;
  44. }
  45. First.PreNode = item;
  46. Last.NextNode = item;
  47. item.PreNode = Last;
  48. item.NextNode = First;
  49. Last = item;
  50. Count++;
  51. }
  52. /// <summary>
  53. /// 通过节点数据添加节点
  54. /// </summary>
  55. /// <param name="item"></param>
  56. public void AddNode(T item)
  57. {
  58. DoubleLinkNode<T> _node = new DoubleLinkNode<T>(item);
  59. AddNode(_node);
  60. }
  61. /// <summary>
  62. /// 移除指定节点
  63. /// </summary>
  64. /// <param name="node"></param>
  65. public void RemoveNode(DoubleLinkNode<T> node)
  66. {
  67. node.PreNode.NextNode = node.NextNode;
  68. node.NextNode.PreNode = node.PreNode;
  69. if (node == First)
  70. {
  71. First = node.NextNode;
  72. }
  73. if (node==Last)
  74. {
  75. Last = node.PreNode;
  76. }
  77. Count--;
  78. }
  79. /// <summary>
  80. /// 清除链表数据
  81. /// </summary>
  82. public void Clear()
  83. {
  84. First = null;
  85. Last = null;
  86. Count = 0;
  87. }
  88. }
  89. /// <summary>
  90. /// 节点类,用来存储数据
  91. /// </summary>
  92. /// <typeparam name="T"></typeparam>
  93. public class DoubleLinkNode<T>
  94. {
  95. public DoubleLinkNode(T item)
  96. {
  97. Value = item;
  98. }
  99. /// <summary>
  100. /// 该节点存储的数据
  101. /// </summary>
  102. public T Value
  103. {
  104. get;
  105. set;
  106. }
  107. /// <summary>
  108. /// 上一个节点
  109. /// </summary>
  110. public DoubleLinkNode<T> PreNode
  111. {
  112. get;
  113. set;
  114. }
  115. /// <summary>
  116. /// 下一个节点
  117. /// </summary>
  118. public DoubleLinkNode<T> NextNode
  119. {
  120. get;
  121. set;
  122. }
  123. }